继续切...这题就是求最长的回文串(忽略奇怪符号..只保留字母,并且不考虑大小写)...枚举中间点..以奇数和偶数的两种情况来搜就可以了...恶心的是有多行..并且多行之间也可以是回文...那就在读的时候将他们都存在一个字符串里..但是' \n ' 也要跟着一起存~~
Program:
/* ID: zzyzzy12 LANG: C++ TASK: calfflac */ #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int ans,i,p,h1,h2,len,j,anslen; char s[20010],str[20010]; int turn(char c) { if (c>='A' && c<='Z') return c-'A'; if (c>='a' && c<='z') return c-'a'; return -1; } int main() { freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout); len=0; while (gets(str)) { p=strlen(str); for (i=0;i<p;i++) s[i+len]=str[i]; len+=p; s[len]='\n'; len++; } ans=0; for (i=0;i<len;i++) { p=1; h1=h2=i; while (h1>=0 && turn(s[h1])==-1) h1--; while (h2<len && turn(s[h2])==-1) h2++; while (h1>=0 && h1<len && turn(s[h1])==turn(s[h2])) { if (p>ans) { ans=p; for (j=h1;j<=h2;j++) str[j-h1]=s[j]; anslen=h2-h1+1; } h1--; while (h1>=0 && turn(s[h1])==-1) h1--; h2++; while (h2<len && turn(s[h2])==-1) h2++; p+=2; } p=2; h1=i; h2=i+1; while (h1>=0 && turn(s[h1])==-1) h1--; while (h2<len && turn(s[h2])==-1) h2++; while (h1>=0 && h1<len && turn(s[h1])==turn(s[h2])) { if (p>ans) { ans=p; for (j=h1;j<=h2;j++) str[j-h1]=s[j]; anslen=h2-h1+1; } h1--; while (h1>=0 && turn(s[h1])==-1) h1--; h2++; while (h2<len && turn(s[h2])==-1) h2++; p+=2; } } printf("%d\n",ans); for (i=0;i<anslen;i++) { if (str[i]!='\n') printf("%c",str[i]); else printf("\n"); } printf("\n"); return 0; }