String---String matching of data structures (KMP algorithm)

Source: Internet
Author: User

String structure exercises--matching strings Time limit:1000ms Memory limit:65536k The topic describes whether string2 is a substring of string1 given two strings string1 and string2. Input input contains multiple sets of data, each set of test data contains two lines, the first row represents string1, and the second line represents STRING2,STRING1 and string2 guarantees that no spaces appear. Output for each set of input data, if string2 is a string1 substring, output "YES", otherwise output "NO". Sample input
Abca12345645abcddd
Sample output
Yesyesno


#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <    Algorithm> #include <stack>using namespace std;void get_next (string t, int next[]) {int J, K; j=0;    K=-1;    Next[0]=-1;    int len=t.size ();            while (J<len) {if (K==-1 | | t[j]==t[k]) {j + +;            k++;        Next[j]=k;    } else k=next[k];    }}int KMP (string s, string t, int next[]) {int I, J; i=0;    j=0;    int len1=s.size ();    int len2=t.size ();            while (I<len1 && j<len2) {if (J==-1 | | s[i]==t[j]) {i++;        j + +;    } else j=next[j];    } if (j==len2) cout<< "yes\n";    else cout<< "no\n"; return 0;}    int main () {string S, T;    int I, J;    int len1, len2;    int next[1000];        while (cin>>s) {cin>>t;        Len1=s.size ();        Len2=t.size ();    Get_next (t, next);    KMP (S, T, next); } return 0;}

String---String matching of data structures (KMP algorithm)

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.