HDU 3068 最長迴文
阿新 • • 發佈:2018-12-18
// manacher演算法 // 步驟 : 1. 處理字串,使其個數變為奇數 // 2. mancher( 3 種 判斷情況) // 細節看程式碼 #include<iostream> #include<cstring> #include<algorithm> using namespace std; char s1[110009*2]; char s2[110009]; int P[110009*2]; int len; void init( ){ int i,j=2; s1[0]='$'; s1[1]='#'; for( i=0;i<len;i++){ s1[j++] = s2[i]; s1[j++] ='#'; } s1[j] ='\0'; len=j; } void manacher(){ int i,id=0,mx=0; P[0]=0; for( i =1;i<=len;i++){ if( mx > i) P[i] = min( P[2*id-i],mx-i); else P[i] = 1; while( s1[i+P[i] ] == s1[ i-P[i] ]) P[i]++; if( mx<P[i]+i){ mx = P[i]+i; id = i; } } } int main(void){ while( scanf("%s",s2) !=EOF){ int ans =-1; len = strlen(s2); init(); manacher(); for( int i=1;i<=len;i++){ ans = max(ans,P[i]); } printf("%d\n",ans-1); } return 0; }