// 题目连接:http://hihocoder.com/problemset/problem/1032
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define N 2000010
char f[N];
int deal(){
int ans = 1;
int len = strlen(f);
for(int i=0;i<len;i++){
int s,e;s = i;e = i;
int t;
while(f[e+1]==f[i]) e++;
i = e;
while(f[s-1]==f[e+1] && s-1>=0) {
s--;e++;
}
if((t = (e - s + 1)) > ans) ans = t;
}
return ans;
}
int main() {
//freopen("in.in","r",stdin);
int T;
scanf("%d",&T);
while(T--){
scanf("%s",f);
printf("%d\n",deal());
}
return 0;
}
·一个挺好的思路:枚举每个子串中间点,向两边扩展,看是否是回文子串。
·注意子串为偶的时候,这里使用while(f[e+1]==f[i]) e++; 来处理。
·这道题的重点在 deal() 函数中的 i = e;这句话,这句话使得 abaaaaaaaaaababa这种情况,中间相同a的部分只需要计算一次,大大减少了特殊情况下的枚举数量