hihocoder1302 最长回文子串

时间:2024-12-18 10:03:07

hihocoder1302 最长回文子串

先贴代码

所有的上面的提示已经交代的好清楚了……

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <climits>
using namespace std;
typedef long long ll;
const double ESP = 10e-;
const int MOD = +;
const int MAXN = +; char Str[MAXN];
char str[MAXN<<];
int f[MAXN<<]; int main(){
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--){
scanf("%s",Str);
int len = strlen(Str);
int n = ;
str[n++] = '$';
str[n++] = '#';
for(int i = ;i < len;i++){
str[n++] = Str[i];
str[n++] = '#';
}
//f[i]真正的回文子串长度+1,也是当前回文串的一半长度+1
str[n] = '\0';
int ans = ;
int mx = ; //不是回文子串的下一个位置
int j;
for(int i = ;i < n;i++){
if(mx > i){
f[i]=min(mx-i,f[*j-i]);
}else{
f[i] = ;
}
while(str[i-f[i] ]==str[i+f[i] ]){
f[i]++;
}
if(f[i]+i > mx){
mx = f[i]+i;
j = i;
}
ans = max(f[i]-,ans); }
printf("%d\n",ans);
}
return ;
}