标签:des style color os strong io for div
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 div
原文地址:http://blog.csdn.net/sunshumin/article/details/38239213