#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
const int maxn=1005;
int next[maxn];
void getnext(char s[],int len){
int j=-1;
next[0]=-1;
for(int i=1;i<len;i++){
while(j!=-1&&s[i]!=s[j+1]){
j=next[j]; //¹Ø¼üËùÔÚ£¡£¡£¡£¡
}
if(s[i]==s[j+1]){
j++;
}
next[i]=j;
}
}
int main(){
char s[maxn];
scanf("%s",s);
getnext(s,strlen(s));
for(int i=0;i<strlen(s);i++)
printf("%d ",next[i]);
return 0;
}
KMP算法之next函数
最新推荐文章于 2023-09-22 22:07:18 发布