您的位置:首页 > 其它

非manacher算法的最长回文串算法

2017-05-31 18:04 316 查看
在hihoCoder上发现了比manacher更快的算法, 看上去很暴力,但跑起来蜜汁快,记录一下贴出来

#include <bits/stdc++.h>
using namespace std;

const int N = 1000000 + 10;
char str
;
int fast(char *str)
{
int ans = 0;
str[0] = '?';
for(int i = 0; str[i]; i++)
{
int s = i, e = i;
while(str[e+1] == str[i]) ++e;
i = e;
while(str[s-1] == str[e+1]) --s, ++e;
ans = max(ans, e - s + 1);
}
return ans;
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
scanf("%s", str + 1);
printf("%d\n", fast(str));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: