在给定的字符串中,我们必须找到一个子字符串,它是回文,并且最长。
要获得最长的回文子串,我们必须解决许多子问题,其中一些子问题是重叠的。它们需要多次解决。因此,动态编程很有用。使用表,我们可以存储先前子问题的结果,并简单地使用它们来生成进一步的结果。
Input: A String. Say “thisispalapsiti” Output: The palindrome substring and the length of the palindrome. Longest palindrome substring is: ispalapsi Length is: 9
findLongPalSubstr(str)
输入-主字符串。
输出-最长回文子串及其长度。
Begin n := length of the given string create a n x n table named palTab to store true or false value fill patTab with false values maxLen := 1 for i := 0 to n-1, do patTab[i, i] = true //as it is palindrome of length 1 done start := 0 for i := 0 to n-2, do if str[i] = str[i-1], then palTab[i, i+1] := true start := i maxLen := 2 done for k := 3 to n, do for i := 0 to n-k, do j := i + k – 1 if palTab[i+1, j-1] and str[i] = str[j], then palTab[i, j] := true if k > maxLen, then start := i maxLen := k done done display substring from start to maxLen from str, and return maxLen End
#include<iostream> using namespace std; int findLongPalSubstr(string str) { int n = str.size(); // get length of input string bool palCheckTab[n][n]; //true when substring from i to j is palindrome for(int i = 0; i<n; i++) for(int j = 0; j<n; j++) palCheckTab[i][j] = false; //initially set all values to false int maxLength = 1; for (int i = 0; i < n; ++i) palCheckTab[i][i] = true; //as all substring of length 1 is palindrome int start = 0; for (int i = 0; i < n-1; ++i) { if (str[i] == str[i+1]) { //for two character substring both characters are equal palCheckTab[i][i+1] = true; start = i; maxLength = 2; } } for (int k = 3; k <= n; ++k) { //for substrings with length 3 to n for (int i = 0; i < n-k+1 ; ++i) { int j = i + k - 1; if (palCheckTab[i+1][j-1] && str[i] == str[j]) { //if (i,j) and (i+1, j-1) are same, then check palindrome palCheckTab[i][j] = true; if (k > maxLength) { start = i; maxLength = k; } } } } cout << "Longest palindrome substring is: " << str.substr(start, maxLength) << endl; return maxLength; // return length } int main() { char str[] = "thisispalapsiti"; cout << "Length is: "<< findLongPalSubstr(str); }
输出结果
Longest palindrome substring is: ispalapsi Length is: 9
我正试图从leet代码中解决一个问题。我为此写了一个方法。这在local Eclipse中工作得很好,但是当我在leetcode上提交这个解决方案时,它说超过了时间限制。 有人能给我一些建议吗?我可以在下面的代码中修改一些东西,以使它更快地工作?我也可以在这个帖子中输入字符串。 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
下面的代码给出了最长的回文子序列长度。如何修改代码以获得最长的回文子串长度? 下面是一个示例调用:
以下是我尝试过的,但在某些情况下失败了,但我觉得我几乎走上了正确的轨道。
我的最新博客地址:我的最新博客 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例 1: 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 示例 2: 输入: "cbbd" 输出: "bb" 实现如下: /** * @param {string} s * @return {string} */
我正在为自己学习DP,我可以轻松解决最长回文子串的长度,但很难真正打印最长回文子串。我检查了一个视频链接,他展示了一种获得LPS的方法,但我无法让它适用于更长的序列。考虑geeksforgeeks的例子: 现在在我的方法中,我将填充表格的底部三角形,如下所示: 因此,对于上述字符串,我的DP表如下所示: 这是视频中原始矩阵的转置,反过来,我不需要单独处理len 1,2, 所以问题是,为什么我在dp
这就是leetcode问题:给定一个字符串s,在s中找到最长的回文子字符串。您可以假定s的最大长度是1000。我的解决方案是使用一个dp表,其中dp[i][j]=以S[i]开始,以S[j]结束的最长回文子字符串的长度 我想知道为什么我的解决方案的时间限制超过了错误,不应该是O(n^2)吗?
为什么JS代码返回“CBB”而不是“BB”?
摘自https://algs4.cs.princeton.edu/53substring/ 15.最长回文子串。给定一个字符串s,找出回文(或Watson-crick回文)中最长的子字符串。 解决方案:可以在线性时间内使用后缀树或Manacher算法解决。这里有一个更简单的解决方案,通常在线性时间内运行。首先,我们描述了如何在线性时间内找到所有长度为L的回文子串:使用Karp-Rabin迭代形成每