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)