#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000005
char str[2 * N + 2] = { 0 };
char st[N] = { 0 };
int p[2 * N + 2] = { 0 };
int func(char* s)
{
int nn = strlen(s);
int n = 2 * nn + 2;
str[0] = '$';//str首位置赋值$
for (int i = 0; i <= nn; i++)
{
str[2 * i + 2] = st[i];
str[2 * i + 1] = '#';
}
int ans = -999;
int mx = 0;//mx用来记录p[i]中到达的最右端的值
int id;//id用来记录p[i]延长至最右端的下标i
for (int i = 1; i<n; i++)
{
if (mx > i)
p[i] = min(p[2 * id - i], mx - i);//不太明白,待深思。。。。
else
p[i] = 1;
while (str[i - p[i]] == str[i + p[i]])
p[i]++;
if (mx < i + p[i])
{
mx = i + p[i];
id = i;
}
if (ans < p[i])
ans = p[i];
}
return ans - 1;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> st;
cout << func(st) << endl;
}
return 0;
}
hipo 4 最长回文
最新推荐文章于 2024-06-24 11:13:20 发布