标签:des style color os strong io for art
Description
求子串的next值,用next数组存放,所有输出
Input
输入一个字符串
Output
输出全部next值
Sample Input
abaabcac
Sample Output
0 1 1 2 2 3 1 2
#include<iostream> #include<string> #include<cstring> using namespace std; int next[10005]; char str[10005]; int len; void getnext(char *str,int next[]) { int j,k; next[1]=0; j=1; k=0; while(j<=len) if((k==0)||(str[j]==str[k])) { ++j; ++k; next[j]=k; } else k=next[k]; } int main() { char s[1005]; cin>>s; len =strlen(s); int j,k; for(j=1,k=0;k<len;j++,k++) { str[j]=s[k]; } int i; getnext(str,next); for(i=1;i<len;i++) cout<<next[i]<<" "; cout<<next[len]<<endl; return 0; }
KMP算法 KMP模式匹配 一(串),布布扣,bubuko.com
标签:des style color os strong io for art
原文地址:http://www.cnblogs.com/mengfanrong/p/3885678.html