#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函數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.