Problem Description
As you know, the goddess likes strings, and in the string, the goddess favorite palindrome string, but not all strings are palindrome strings, but some strings can be "derivative" to become a palindrome string.
The string contains only lowercase letters.
The derivation process is as follows, C + +:
String dif (const string x) { if (x.length () <=1) return ""; String res= ""; for (int i=1;i<x.length (); ++i) res+=abs (x[i]-x[i-1]) + ' a '; return res;}
C:
void dif (char*x,char*res)//Note that it is possible to overflow { if (x[0]==0| | x[1]==0) { res[0]=0; return; } int len=1; for (int i=1;x[i];++i,++len) res[i-1]=abs (x[i]-x[i-1]) + ' a '; res[len-1]=0;}
For example, the "AA" string is "a", "AaB" the lead string is "AB", "AACFWSSG" the string is "Acdream".
So given a string, determine how long is the longest palindrome string in its various order strings?
The second-order guide string is the lead string of the string.
The N-order string is the lead string of the n-1 order derivative.
Input multiple sets of data, each set of data including a string s (1<=|s|<=1000) output for each set of data, an integer sample input
Abcdabcbaacdream
Sample Output
353
Hint
Example one, after the string is called "AAA", the longest palindrome substring is itself, so the length is 3
Example two, itself is a palindrome string, so the output itself can be
Example Three, acdream->cbonem->bnbji->mmib->aeh->ed->b
The longest palindrome lengths are 1,1,3,2,1,1,1, so the output is 3
Problem Solving Ideas:
The core is to seek the largest palindrome string, but is not directly asked, is the requirements of the largest palindrome after the difference, this problem is due to small data, directly from itself to seek the difference, call the Manacher algorithm can find the largest palindrome string, and then ask the largest can be!
AC Code:
#include <iostream>#include<string>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Charss[2005],s[2005],res[3005];intp[2005];voidDifChar*s,Char*Res) { if(s[0]==0|| s[1]==0) {res[0]=0; return; } intlen=1; for(intI=1; s[i];++i,++len) res[i-1]=abs (s[i]-s[i-1])+'a'; Res[len-1]=0;}intManacher (Char*s) { intLen = strlen (s), Maxp =0, ans =0; for(inti = len; I >=0; i--) {s[i*2+2] =S[i]; S[i*2+1] ='#'; } s[0] ='*'; for(inti =2; I <2* Len +1; i++) { if(P[maxp] + MAXP >i) p[i]= Min (p[2* Maxp-i], P[MAXP] + MAXP-i); ElseP[i]=1; while(S[i-p[i]] = = S[i +P[i]]) P[i]++; if(P[maxp] + Maxp < i +P[i]) Maxp=i; if(Ans <P[i]) ans=P[i]; } returnAns-1;}intMain () {intMax1; while(gets (s)) {Max1=0; while(strlen (s) >1) {memset (P,0,sizeof(p)); strcpy (ss,s); Max1=Max (Max1,manacher (ss)); DIF (s,res); strcpy (S,res); if(Max1>=strlen (s)) Break; } printf ("%d\n", MAX1); } return 0;}
[Acdream] Goddess teaches you string--string