kmp复健,答案是n-next[n]
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int n,ne[N];
char s[N];
int main()
{
scanf("%d%s",&n,s+1);
int j=0;
for(int i=2;i<=n;i++)
{
while(s[j+1]!=s[i]&&j)
j=ne[j];
if(s[j+1]==s[i])
j++;
ne[i]=j;
}
printf("%d\n",n-ne[n]);
return 0;
}
原文地址:https://www.cnblogs.com/lokiii/p/9607322.html
时间: 2024-10-15 05:04:58