枚举+标记即可。
/* ID: cuizhe LANG: C++ TASK: calfflac */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <map> #include <algorithm> using namespace std; char str[30001],p[30001]; int o[30001]; int main() { int i,j,len,num,ans,t1,t2,st,end; char ch; freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout); i = 0; while(scanf("%c",&ch)!=EOF) { str[i] = ch; i ++; } len = i-1;j = 0; for(i = 0;i <= len;i ++) { if(str[i] <= 'z'&&str[i] >= 'a') { o[j] = i; p[j++] = str[i]; } else if(str[i] <= 'Z'&&str[i] >= 'A') { o[j] = i; p[j++] = str[i] + 32; } } num = j-1;ans = 0; for(i = 0;i <= num;i ++) { int temp = 0; st = i-1; end = i; while(st >= 0&&end <= num) { if(p[st] == p[end]) temp += 2; else break; st --; end ++; } if(ans < temp) { ans = temp; t1 = o[st+1]; t2 = o[end-1]; } temp = 1; st = i-1; end = i+1; while(st >= 0&&end <= num) { if(p[st] == p[end]) temp += 2; else break; st --; end ++; } if(ans < temp) { ans = temp; t1 = o[st+1]; t2 = o[end-1]; } } printf("%d\n",ans); for(i = t1;i <= t2;i ++) { printf("%c",str[i]); } printf("\n"); return 0; }