专题:kmp&manacher算法
有两题贼神奇,巧用Next[]看不懂,看了半天也理解不了,先记下来以后再慢慢看;
有一篇介绍Next[]数组的:http://www.cnblogs.com/c-cloud/p/3224788.html
题目链接:https://cn.vjudge.net/contest/247288#problem/F
Question_F: 参考
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char str[1000010];
int Next[1000010];
bool f[1000010];
void next_arr()
{
int len = strlen(str);
Next[0] = -1;
int k = -1;
int j = 0;
while (j < len )
{
if (k == -1 || str[j] == str[k])
{
++k;
++j;
Next[j] = k;
}
else
k = Next[k];
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(Next,0,sizeof(Next));
scanf("%s",str);
next_arr();
int n=strlen(str);
memset(f,false,sizeof(f));
int tmp = n;
while(tmp > 0)
{
if(n >= 2*tmp)
f[tmp] = true;
tmp = Next[tmp];
}
int ans = 0;
for(int i = n-1;i > 1;i--)
{
tmp = i;
while(tmp > 0)
{
if(f[tmp] && i >= 2*tmp && n >= i+tmp)
{
ans = max(ans,tmp);
break;
}
tmp = Next[tmp];
}
}
printf("%d\n",ans);
}
}
G题也挺好的,也是巧用Next[数组];
马拉车算法模板:https://blog.csdn.net/explormt/article/details/81742864
最大最小表示法(我还没写到用的题):https://blog.csdn.net/zy691357966/article/details/39854359