字符串最长回文子串_最长回文子串
字符串最長回文子串
Problem statement:
問題陳述:
Given a string str, find the longest palindromic substring. A substring need to be consecutive such that for any xixj i<j must be valid in the parent string too. Like "incl" is a substring of "includehelp" while "iel" is not
給定字符串str ,找到最長回文子字符串。 子字符串必須是連續的,以便對于任何x i x j i <j在父字符串中也必須有效。 像“ incl”是“ includehelp”的子字符串,而“ iel”不是
Input:
輸入:
The first line of input contains an integer T, denoting the no of test cases then T test cases follow. Each test case contains a tring str.
輸入的第一行包含一個整數T ,表示測試用例的數量,然后是T個測試用例。 每個測試用例都包含一個tring str 。
Output:
輸出:
For each test case output will be a string which is the longest palindromic substring could be formed from the string str. There can be many valid answers, all are correct.
對于每個測試用例,輸出將是一個字符串,該字符串是可以從字符串str形成的最長回文子字符串。 可能有許多有效答案,所有答案都是正確的。
Constraints:
限制條件:
1 <= T <= 100 1 <= length of string str <= 300Example:
例:
Input: test case:2First test case: Input string: "aaaa"Output: Longest palindromic substring is: "aaaa"Second test case: Input string: "abcaba"Output: Total count of palindromic sub-sequence is: "aba"Explanation:
說明:
Test case 1: Input: "aaaa"
測試案例1:輸入:“ aaaa”
The valid palindromic substrings are shown below:
有效回文子串如下所示:
Marked cells are character taken in subsequence:
標記的單元格是子序列中的字符:
So the longest one is "aaaa"
所以最長的是“ aaaa”
For the second test case,
對于第二個測試用例,
The substrings can be, "a" "b" "c" "aba"So the longest one is "aba"Solution approach
解決方法
This can be solved by using DP top-down approach,
這可以通過使用DP自上而下的方法來解決,
Initialize a Boolean 2D array dp[n][n] to store whether the substrings are palindrome or not.
初始化布爾2D數組dp [n] [n],以存儲子字符串是否為回文。
Initialize max to store max length of substring and p to store the substring itself.
初始化max來存儲子字符串的最大長度,并初始化p來存儲子字符串本身。
Initially,
原來,
p is the first character as that's the smallest palindrome of length 1 and max = 1.
p是第一個字符,因為它是長度為 1且max = 1的最小回文。
Fill up the base case,
填滿基本情況
Base case is that each single character is a palindrome itself. And for length of two, i.e, if adjacent characters are found to be equal then
基本情況是,每個字符本身都是回文。 對于兩個字符的長度,即如果發現相鄰字符相等,則
dp[i][i+1]=true, else if characters are different then dp[i][i+1]=false
dp [i] [i + 1] = true ,否則如果字符不同則dp [i] [i + 1] = false
To understand this lets think of a string like "acaa"
要理解這一點,可以考慮一個字符串,例如“ acaa”
Here
這里
dp[0][1]=false
dp [0] [1] = false
Whereas for
鑒于
dp[2][3] =true
dp [2] [3] = true
for i=0 to n// for single length charactersdp[i][i]=true if(i==n-1)break; if(s[i]==s[i+1])dp[i][i+1]=trueelsedp[i][i+1]=false; end forCompute for higher lengths,
計算更長的長度,
for len=3 to nfor start=0 to n-lenint end=start+len-1;// start and end is matching and rest of // substring is already palindromeif(s[end]==s[start] && dp[start+1][end-1])dp[start][end]=true;Update max=len;Update p=s.substr(start,len);elsedp[start][end]=false;end ifend for end forFinal result is stored in p;
最終結果存儲在p中 ;
So for higher lengths, if starting and ending index is the same then we recur for the remaining characters since we have the sub-problem result stored so we computed that. That's why we just update max to len as we are checking for increasing length at each iteration.
因此,對于更大的長度,如果開始索引和結束索引相同,則將重復其余字符,因為我們已存儲了子問題結果,因此我們對其進行了計算。 這就是為什么我們在每次迭代中檢查長度增加時僅將max更新為len的原因。
For proper understanding, you can compute the table by hand for the string "abcaba" to understand how it's working.
為了正確理解,您可以手動計算字符串“ abcaba”的表以了解其工作方式。
C++ Implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;string longestPalindrome(string s) {int n = s.length();if (n == 0)return "";if (n == 1)return s;bool dp[n][n];string p = string(1, s[0]);int max = 1;for (int i = 0; i < n; i++) {dp[i][i] = true;if (i < n - 1 && s[i] == s[i + 1]) {dp[i][i + 1] = true;if (2 > max) {p = s.substr(i, 2);max = 2;}}else if (i < n - 1) {dp[i][i + 1] = false;}}for (int len = 3; len <= n; len++) {for (int j = 0; j <= n - len; j++) {int end = j + len - 1;if (s[j] == s[end]) {dp[j][end] = dp[j + 1][end - 1];if (dp[j][end] && len > max) {max = len;p = s.substr(j, len);}}elsedp[j][end] = false;}}return p; }int main() {int t;cout << "Enter number of testcases\n";cin >> t;while (t--) {string str;cout << "Enter the string\n";cin >> str;cout << "Longest palindromic substring: " << longestPalindrome(str) << endl;}return 0; }Output:
輸出:
Enter number of testcases 2 Enter the string anccnakj Longest palindromic substring: anccna Enter the string abcaba Longest palindromic substring: aba翻譯自: https://www.includehelp.com/icp/longest-palindromic-substring.aspx
字符串最長回文子串
總結
以上是生活随笔為你收集整理的字符串最长回文子串_最长回文子串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 送一件快递多少钱啊?
- 下一篇: 小米电视和红米哪个质量好不好?