B-KMP mode matching 2 (string)
Crawling in process...
Crawling failed
Time limit:1000 ms
Memory limit:131072kb
64bit Io format:% LLD & % LlU
Description
Enter a primary string and a sub-string, and use KMP for matching. If the match fails, the output is 0.
Input
Enter a main string and a sub-string
Output
Number of matched shards
Sample Input
ababcabcacbababcac
Sample output
3
I read the code this morning. Evaluate the next array ..
#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;}elsek=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;}