记录一下吧
回文代码如下:
#include <cstdio>
#include <cstring>
#include <string.h>
char str[1000002 + 1200];
int fast(char *p)
{
int ans = 1;
for (int i = 1; p[i]; ++i)
{
int s = i, e = i, t;
while (p[e + 1] == p[i]) ++e;
i = e;
while (p[s - 1] == p[e + 1]) --s, ++e;
if ((t = e - s + 1) > ans) ans = t;
}
return ans;
}
int main()
{
str[0]='$';
int n;
scanf("%d", &n);
while (n --) {
scanf("%s", str + 1);
printf("%d\n", fast(str));
}
return 0;
}
改进一下就是双倍回文了:
#include <bits/stdc++.h>
using namespace std;
char str[500002 + 1200];
vector <int> E[500010];
int fast(char *p)
{
int ans = 0;
for (int i = 1; p[i]; ++i)
{
int s = i, e = i;
while (p[e + 1] == p[i])
{
++e;
E[i].push_back(e);
if((e-i+1)%4 == 0)
ans = max(ans, e-i+1);
}
i = e;
while (p[s - 1] == p[e + 1])
{
--s;
++e;
E[s].push_back(e);
if((e-s+1)%4==0)
{
int mid = s+(e-s)/2;
int f = 0, n = E[s].size();
for(int j = 0; j < n; j++)
if(E[s][j] == mid)
{
f = 1;
break;
}
if(f)
ans = max(ans, e-s+1);
}
}
}
return ans;
}
int main()
{
str[0]='$';
int n;
scanf("%d", &n);
scanf("%s", str + 1);
printf("%d\n", fast(str));
return 0;
}