dp
Note that no statement S is not empty, deal with it
o (n ^ 2)
class Solution { public: string longestPalindrome(string s) { if (s.empty()) return ""; int len=s.length(); int dp[len][len]; for(int i=0;i<len;i++) for(int k=0;k<len;k++) dp[i][k]=0; int start=0,end=0; for (int i=0;i<len;i++) { dp[i][i]=1; if((i<len-1)&&(s[i]==s[i+1])){ dp[i][i+1]=1; start=i; end=i+1; } } for ( int DIS = 2 ; DIS <len; DIS ++) // I-> the I-. 1, the I +. 1, it can not process two consecutive { for ( int I = 0 ; (I + DIS) <len; I ++ ) IF ((DP [I + . 1 ] [I + dis- . 1 ] == . 1 ) && (S [I] == S [I + DIS])) { dp[i][i+dis]=1; if((dis)>(end-start)){ start=i; end=i+dis; } } } return s.substr(start,end-start+1); } };
Problems encountered:
= == written. . . . .
Then dp 0 array without first mem ...
Then Manacher law
Reference https://www.cnblogs.com/mini-coconut/p/9074315.html
First, Manacher algorithm provides a clever way, and the length of palindromic sequence is palindromic sequence length of odd-even number considered together,
In practice, in the middle of each adjacent two of the original character string is inserted into a separator, while also adding a end to end delimiter, the delimiter is not required to appear in the original string, under normal circumstances may be using a #. Here is an example:
(1) Len array Introduction and nature
Rightmost character Manacher algorithm with an auxiliary array Len [i] in characters T [i] as the center of the longest string of the palindrome T [i] of length, such as in T [i] as the center of the longest character back string is T [l, r], then Len [i] = r-i + 1.
For the above example, can be derived Len [i] array is:
Len array has a property that Len [i] -1 is the palindromic substring in the original string of length S,
prove,
T is first converted character string, all lengths are palindromic string is odd, for the longest string in a palindrome T [I] as the center of its length is of 2 * Len [i] -1, observe that after, T palindromic all substrings, wherein the number of separators over a certain number than other characters, there is Len [i] delimiters, the remaining Len [i] -1 characters from the original character string, so that the palindromic sequence in the original string length is to Len [i] -1.
With this property, the original problem is transformed for the sake of all of Len [i]. Here's how within linear time complexity of obtaining all of Len.
Computing (2) Len array
First from left to right are sequentially calculated Len [i], when the calculated Len [i], Len [j] (0 <= j <i) is calculated has been completed.
Let P be calculated before the right end of the longest sub-palindromic sequence, and arranged to obtain the maximum position po, two cases:
The first case: i <= P
Then find the symmetrical positions with respect to the po i of set j, then if Len [j] <Pi, as shown below:
Then that certain palindromic sequence in the interior of the centering po j centered at palindromic sequence, and i and j on symmetrical positions po,
Found palindromic sequence defined by, in turn, a palindromic sequence or a palindromic sequence,
Therefore, in the center of the length i is palindromic sequence at least palindromic sequence and j is a center of the same (since j, i on the point P and its vicinity symmetry, where the palindromic sequence symmetrical past j), i.e., Len [i]> = Len [j].
Because Len [j] <Pi, so that i + Len [j] <P. By symmetry understood Len [i] = Len [j].
If Len [j]> = Pi, by the symmetry described by i centered palindromic sequence may extend beyond the P, and P is larger than the part we do not match, starts from a position P + 1 a match until a mismatch occurs, and to update the corresponding po P and Len [i].
Second case: i> P
If i is larger than P, as described midpoint i palindromic sequence also no match at all, at this time, a match can only honestly, and after completion of the matching to update the position P and the corresponding po and Len [i].
2. Time complexity analysis
Manacher time complexity of algorithm analysis and algorithm similar to Z, because when the position matching algorithm has not only met matches, is already matched position no longer match, so for T string in each location, only a match, the overall time complexity of the algorithm is O Manacher (n), where n T is the length of the string, since the length T in fact twice as S, the time complexity is still linear.
Here is the algorithm, note that in order to avoid the update time lead to cross-border P, we add a special character in front of a string T, for example '$', so the algorithm strings are 1-based. ,