Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.
Given the string s, find its longest palindrome substring. Suppose that the longest length of S is 1000 and only one eldest string.
This problem has a variety of solutions, you can use dynamic programming, the formula is: P (i, j) = P (i+1, J-1) && (s[i] = = S[j]), according to the length limit, the auxiliary space can be statically set to 1000*1000, time complexity is O (n*n),
Space is also O (n*n), can also be traversed from one side to the other, until the discovery of asymmetric characters, it is important to note that Palindrome Sub-center characters and no central character two cases, respectively consider the line, time complexity is O (n*n),
But the space only needs O (1), so this article uses the second, the code is as follows:
stringLongestpalindrome (strings) {intn =s.size (); stringret; for(intI=0; i<n; ++i) {intL = i, R =i; while(l>=0&& r<n && s[l] = =S[r]) { --l; ++R; } ++l; --R; if(Ret.size () < r-l+1) ret= S.substr (L, r-l+1); L= i; R = i+1; while(l>=0&& r<n && s[l] = =S[r]) { --l; ++R; } ++l; --R; if(Ret.size () < r-l+1) ret= S.substr (L, r-l+1); } returnret;}
5. Longest palindromic Substring