KMP algorithm (string)

Source: Internet
Author: User

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;}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.