标签:code case script 最长回文串 const manacher can class names
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
Sample Input
aaaa abab
Sample Output
4 3
当做是Manacher算法的模板题吧...估计实用价值和KMP没什么区别
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=250010; 4 char s[N],ss[N]; 5 int RL[N]; 6 int calc(){ 7 ss[0]=‘#‘; 8 for(int i=1,i_end=strlen(s);i<=i_end;++i){ 9 ss[(i<<1)-1]=s[i-1]; 10 ss[(i<<1|1)-1]=‘#‘; 11 } 12 int len=strlen(s)<<1|1; 13 int MaxRight=0,pos=0,maxlen=0; 14 for(int i=0;i<=len;++i){ 15 if(i<MaxRight)RL[i]=min(RL[(pos<<1)-i],MaxRight-i); 16 else RL[i]=1; 17 18 while(i-RL[i]>=0&&i+RL[i]<=len&&ss[i-RL[i]]==ss[i+RL[i]])++RL[i]; 19 if(RL[i]+i-1>MaxRight) 20 MaxRight=RL[i]+i-1,pos=i; 21 maxlen=max(maxlen,RL[i]); 22 } 23 return maxlen-1; 24 } 25 int main(){ 26 while(~scanf("%s",s)) 27 printf("%d\n",calc()); 28 return 0; 29 }
标签:code case script 最长回文串 const manacher can class names
原文地址:http://www.cnblogs.com/ndqzhang1111/p/6905732.html