Longest symmetric substring
For a given string, the subject asks you to output the length of the longest symmetric substring. For example, given that the longest Is PAT&TAP symmetric?
symmetric substring is s PAT&TAP s
, then you should output 11.
Input format:
The input gives a non-empty string with a length of not more than 1000 in a row.
Output format:
Outputs the length of the longest symmetric substring in a row.
Horse-drawn Car algorithm:
a) the first step is to transform the string s into T, and its transformation method is as follows:
insert a "#" between the characters of the string s and the end of the S, such as: S= "ABBA" into t= "#a #b#b#a#". We will find that the length of S is 4, and the length of T is 9, the length becomes odd!! The length of that s is
In odd cases, is the length of the change still odd? For example, s= "ABCBA", which changes to t= "#a #b#c#b#a#", the length of T is 11, so we find that the purpose of the transformation is to change the length of the string
is an odd number, so you can handle the parity situation uniformly .
Second step, in order to improve the case of palindrome overlap, we will transform the t[i] at the palindrome radius stored in the array p[], p[i] for the new string T t[i], the palindrome radius , denoted by the character T[i] as the center
The maximum right character of the longest back text string to T[i], such as the length of the longest palindrome string centered on t[I, t[L, R], then p[i]=r-i+1. So the last traversal of the array p[], take the maximum value. If p[i]=1
Indicates that the palindrome is t[i] itself. Take a simple example and feel:
The array P has a property, p[I]-1 is the palindrome substring in the original string s length, that is p[i]-1 is the palindrome substring in the original string s length, as to prove, first in the converted string T, all
The length of the text string is odd, then for the t[i] as the center of the longest back text string, its length is 2*p[i]-1, after observation, T in all palindrome substrings, wherein the number of separators must be more than the other characters
The number is 1, that is, there are p[i] separators, the remaining p[i]-1 characters from the original string, so the palindrome string in the original strings in length is p[i]-1.
In addition, because the first and last characters are the # number, and also need to search palindrome, in order to prevent cross-border, we also need to add a non-# character, the actual operation we only need to add a non-# character at the beginning, the end is not added because the end of the string is identified as ' ", the default is added. So the original problem is transformed into how to find the array p[] problem.
c) How to find the array p []
From left to right, the array p[], MI is the center of the maximum palindrome string, and R is the most right-hand value to reach the maximum palindrome string.
1) When I <=r, how to calculate the value of p[i]? There is no doubt that the value of the point before the midpoint of the array P has been calculated. Using the characteristics of palindrome string, we find the point I about Mi symmetric point J, its value
For j= 2*mi-i. Because, point J, I in the range of the largest palindrome with Mi as the center ([L, R]),
A) Then if p[j] <r-i (also the distance between L and J), it is stated that the Palindrome string centered on point J is not out of range [L, R], which is known by the characteristics of palindrome, which is traversed from the left and right sides to MI, and the corresponding
The characters are equal. So p[J]=p[i](here we have to go from Point J to the case of I), such as:
b) If p[J]>=r-i (that is, J is the center of the left end of the palindrome string more than L), as shown. That is, the range of the largest palindrome centered on point J has gone beyond the range [L, R], and in this case, the equation p[J]=p[I] is also established? Obviously not always set up! Because, the left end of the palindrome string centered on point J is more than L, then the characters between [L, J] must be found equal in (J, Mi], by the characteristics of the palindrome, p[i] is at least equal to
R-i, as to whether it is greater than r-i (the red part of the figure), we also need to start from the r+1 a match, direct mismatch so far, so as to update R and the corresponding MI and p[i].
2) When I > R, such as. This situation, can not take advantage of the characteristics of palindrome string, only honest step by step to match.
AC Code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<vector>#include<Set>#include<map>#include<stack>#include<queue>using namespacestd;Const intn=3010;CharA[n];CharRes[n];intP[n];//int dp[n][n];intLen;intManacher (Chara[]) {res[0]='$'; res[1]='#'; Len=strlen (a); inttemp=2; ///* Transform String * * for(intI=0; i<len;i++) {res[temp++]=A[i]; Res[temp++]='#'; } //cout<<res<<endl; intLen2=strlen (RES); Memset (P,0,sizeof(p)); //MI is the center point corresponding to the maximum palindrome string, right is the value that the palindrome can reach. intMi=0, right=0; //MaxLen is the length of the maximum palindrome string, MaxPoint is the record center point intMaxlength=0, maxpoint=0; for(intI=1; i<len2;i++) {P[i]=right>i?min (p[2*mi-i],right-i):1; while(res[i+p[i]]==res[i-P[i]]) P[i]++; //above the right end, change the center point and the corresponding right end if(right<i+P[i]) { Right=i+P[i]; Mi=i; } //update the length of the maximum palindrome string and note the points at this point if(maxlength<P[i]) {MaxLength=P[i]; MaxPoint=i; } } returnmaxlength;}intMain () {gets (a); intans=Manacher (a); printf ("%d", ans-1);}
Ladder Cup l2-008. Longest symmetric substring (horse cart algorithm application)