最长回文子串
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<queue> #include<vector> #include<cstring> #include<cmath> #include<map> using namespace std; typedef long long ll; #define N 2011111 char s1[N],s[N]; int p[N],len; void init() { for(int i=0; i<len; i++){ s[i*2]='*'; s[i*2+1]=s1[i]; } s[len*2]='*'; len=len*2+1; } int sk() { int mx=-1,id,ss=-1; for(int i=0;i<len;i++){ if(mx>i)p[i]=min(p[2*id-i],mx-i); else p[i]=1; while(s[i-p[i]]==s[i+p[i]]&&i-p[i]>=0&&i+p[i]<len)p[i]++; if(mx<i+p[i]){ mx=i+p[i]; id=i; } ss=max(ss,p[i]); } //for(int i=0;i<len;i++)printf("%3c",s[i]);cout<<endl; //for(int i=0;i<len;i++)printf("%3d",p[i]);cout<<endl; return ss-1; } int main() { int n; scanf("%d",&n); while(n--){ scanf("%s",&s1); len=strlen(s1); init(); printf("%d\n",sk()); } return 0;
}