原文请访问我的博客:
xiaoshig.sinaapp.com
KMP模式匹配 一(串)
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Description
求子串的next值,用next数组存放,全部输出
Input
输入一个字符串
Output
输出所有next值
Sample Input
abaabcac
Sample Output
0 1 1 2 2 3 1 2
#include<iostream> #include<cstring> using namespace std; int a[100000]; char b[100000]; int main() { int j,n,i,k; cin>>b; a[0]=-1; j=-1; i=0; while(b[i]!='\0') { if(j==-1||b[j]==b[i]) { j++;i++; // if(b[i]!=b[j]) a[i]=j; // else // {if(a[j]==-1) // a[i]=0; // else // a[i]=a[j];} } else j=a[j]; } i=1; cout<<a[0]+1; while(b[i]!='\0') { cout<<' '<<a[i]+1; i++; } return 0; }
&spm=1001.2101.3001.5002&articleId=38265809&d=1&t=3&u=7529057a4bd24c83889f46bf8caa52ba)
1019

被折叠的 条评论
为什么被折叠?



