PAT Advanced Level 1040. Longest Symmetric String (25)
水题。枚举中间,暴力算最远的。也可以用(Manacher)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right>=len||s[left]!=s[right])) left--,right++,sum=sum+2; ans=max(ans,sum); } int main() { gets(s); len=strlen(s); ans=0; for(int i=0;i<len;i++) check(i,i),check(i,i+1); printf("%d\n",ans); return 0; }