4.21 回文串 回文序列

This commit is contained in:
zhangsan 2025-04-21 16:21:26 +08:00
parent f29842be95
commit 263bc7481f
8 changed files with 227 additions and 1 deletions

View File

@ -0,0 +1,44 @@
package dynamic_programming;
/**
* 题目 647. 回文子串 (longestCommonSubsequence)
* 描述给你一个字符串 s 请你统计并返回这个字符串中 回文子串 的数目
* 回文字符串 是正着读和倒过来读一样的字符串
* 子字符串 是字符串中的由连续字符组成的一个序列
* 示例 1
输入s = "abc"
输出3
解释三个回文子串: "a", "b", "c"
* 链接https://leetcode.cn/problems/palindromic-substrings/
*/
//不会
public class CountSubstrings {
/**
*布尔类型的dp[i][j]表示区间范围[i,j] 注意是左闭右闭的子串是否是回文子串
* 情况一下标i j相同同一个字符例如a当然是回文子串
* 情况二下标i j相差为1例如aa也是回文子串
* 情况三下标i j相差大于1的时候例如cabac此时s[i]与s[j]已经相同了我们看i到j区间是不是回文子串就看aba是不是回文就可以了那么aba的区间就是 i+1 j-1区间这个区间是不是回文就看dp[i + 1][j - 1]是否为true
* 递推顺序为左下到右上
*/
public int countSubstrings(String s) {
char[] chars = s.toCharArray();
int len = chars.length;
boolean[][] dp = new boolean[len][len];
int result = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j < len; j++) {
if (chars[i] == chars[j]) {
if (j - i <= 1) { // 情况一 情况二
result++;
dp[i][j] = true;
} else if (dp[i + 1][j - 1]) { //情况三
result++;
dp[i][j] = true;
}
}
}
}
return result;
}
}

View File

@ -10,8 +10,36 @@ package dynamic_programming;
* 链接https://leetcode.cn/problems/maximum-length-of-repeated-subarray/ * 链接https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
*/ */
//不会
/**
* dp[i][j] 的定义是
*
* nums1[i-1] nums2[j-1] 作为结尾元素的两个前缀序列分别是 nums1[0i-1] nums2[0j-1]的最长公共后缀subarray的长度
* 这样就可能出现dp[1][1]=1 dp[1][2]=0的情况了
*
* 状态转移方程
* if (nums1[i-1] == nums2[j-1])
* dp[i][j] = dp[i-1][j-1] + 1;
* else
* dp[i][j] = 0;
*/
public class FindLength { public class FindLength {
public int findLength(int[] nums1, int[] nums2) { public int findLength(int[] nums1, int[] nums2) {
return 0; int n = nums1.length, m = nums2.length;
// dp[i][j] 表示 nums1[0..i-1] nums2[0..j-1] '结尾'处的最长公共后缀长度
int[][] dp = new int[n + 1][m + 1];
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
ans = Math.max(ans, dp[i][j]);
}
// else dp[i][j] 默认为 0
}
}
return ans;
} }
} }

View File

@ -0,0 +1,30 @@
package dynamic_programming;
/**
* 题目 1143. 最长公共子序列 (longestCommonSubsequence)
* 描述给定两个字符串 text1 text2返回这两个字符串的最长 公共子序列 的长度如果不存在 公共子序列 返回 0
* 一个字符串的 子序列 是指这样一个新的字符串它是由原字符串在不改变字符的相对顺序的情况下删除某些字符也可以不删除任何字符后组成的新字符串
* 例如"ace" "abcde" 的子序列 "aec" 不是 "abcde" 的子序列
* 两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列
* 示例 1
输入text1 = "abcde", text2 = "ace"
输出3
解释最长公共子序列是 "ace" 它的长度为 3
* 链接https://leetcode.cn/problems/longest-common-subsequence/
*/
public class LongestCommonSubsequence {
public int longestCommonSubsequence(String text1, String text2) {
int n=text1.length(),m=text2.length();
int[][]dp=new int[n+1][m+1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if(text1.charAt(i-1)==text2.charAt(j-1))
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
return dp[n][m];
}
}

View File

@ -0,0 +1,38 @@
package dynamic_programming;
/**
* 题目 5. 最长回文子串 (longestPalindrome)
* 描述给你一个字符串 s找到 s 中最长的 回文 子串
*
* 示例 1
输入s = "babad"
输出"bab"
解释"aba" 同样是符合题意的答案
* 链接https://leetcode.cn/problems/longest-palindromic-substring/
*/
public class LongestPalindrome {
public String longestPalindrome(String s) {
int n = s.length();
if (n < 2) return s;
char[] cs = s.toCharArray();
boolean[][] dp = new boolean[n][n];
int maxLen = 1, start = 0;
// 从后往前遍历 ij i 往后
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
// 回文条件首尾相等且要么长度<=2两字符或单字符要么内部也是回文
if (cs[i] == cs[j] && (j - i <= 1 || dp[i + 1][j - 1])) {
dp[i][j] = true;
int len = j - i + 1;
if (len > maxLen) {
maxLen = len;
start = i;
}
}
}
}
return s.substring(start, start + maxLen);
}
}

View File

@ -0,0 +1,38 @@
package dynamic_programming;
/**
* 题目 516. 最长回文子序列 (longestPalindromeSubseq)
* 描述给你一个字符串 s 找出其中最长的回文子序列并返回该序列的长度
* 子序列定义为不改变剩余字符顺序的情况下删除某些字符或者不删除任何字符形成的一个序列
* 示例 1
输入s = "bbbab"
输出4
解释一个可能的最长回文子序列为 "bbbb"
* 链接https://leetcode.cn/problems/longest-palindromic-subsequence/
*/
public class LongestPalindromeSubseq {
public int longestPalindromeSubseq(String s) {
int n = s.length();
if (n < 2) return n;
char[] cs = s.toCharArray();
int[][] dp = new int[n][n];
// 对角线全是长度 1 的子序列
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
// i 从后往前确保 dp[i+1][*] 已经算好
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (cs[i] == cs[j]) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][n - 1];
}
}

View File

@ -0,0 +1,16 @@
package dynamic_programming;
import org.junit.Test;
import static org.junit.Assert.*;
public class LongestCommonSubsequenceTest {
@Test
public void longestCommonSubsequence() {
String text1 = "aab",text2 = "abb";
LongestCommonSubsequence solution = new LongestCommonSubsequence();
int res=solution.longestCommonSubsequence(text1,text2);
System.out.println(res);
}
}

View File

@ -0,0 +1,16 @@
package dynamic_programming;
import org.junit.Test;
import static org.junit.Assert.*;
public class LongestPalindromeSubseqTest {
@Test
public void longestPalindromeSubseq() {
LongestPalindromeSubseq solution = new LongestPalindromeSubseq();
String s="abc";
int res=solution.longestPalindromeSubseq(s);
System.out.println(res);
}
}

View File

@ -0,0 +1,16 @@
package dynamic_programming;
import org.junit.Test;
import static org.junit.Assert.*;
public class LongestPalindromeTest {
@Test
public void longestPalindrome() {
LongestPalindrome solution = new LongestPalindrome();
String s = "babad";
String S=solution.longestPalindrome(s);
System.out.println(S);
}
}