题意:求最长回文子串的长度
代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 1000000 * 2 + 50;
int dis[maxn];
char str1[maxn],str2[maxn];
int main(){
int ca = 0;
while(~sf("%s",str1) && str1[0] != 'E'){
int len = strlen(str1);
str2[0] = '$';
char* str_a = str2 + 1;
for(int i = 0;i <= len;++i){
str_a[i * 2] = '#';
str_a[i * 2 + 1] = str1[i];
}
// puts(str2);
int id = 0, mx = 1,len2 = strlen(str2);
int ans = 0;
for(int i = 1;i < len2;++i){
if(mx > i){
dis[i] = (dis[id * 2 - i] < (mx - i) ? dis[2 * id - i] : (mx - i));
}
else dis[i] = 1;
while(str2[i - dis[i]] == str2[i + dis[i]]) dis[i]++;
if(i + dis[i] > mx){
mx = i + dis[i];
id = i;
}
ans = max(ans,dis[i]);
}
pf("Case %d: %d\n",++ca,ans - 1);
}
return 0;
}