Binary String matching time limit: theMs | Memory Limit:65535KB Difficulty:3
-
-
Descriptive narrative
-
Given strings A and B, whose alphabet consist only ' 0 ' and ' 1 '. Your task is-only to-tell how many times does A appear as a substring of B?
For example, the text string B is ' 1001110110 ' and the pattern string A is ' one ', you should output 3, because the patter N A appeared at the posit
-
-
Input
-
The first line consist only one
-
integer n, indicates n cases follows. In each case, there is lines, the first line gives the string A, Length (a) <=, and the second line gives the S Tring B, Length (b) <= 1000. And it is guaranteed this B is always longer than A.
-
-
Output
-
-
for each case, the output a single line consist a single integer, tells how the many times do B appears as a substring of a.
-
-
Example input
-
-
-
-
Example output
-
-
-
/******KMP algorithm *********/#include <cstdio> #include <cstring> #include <iostream> #include <cmath > #include <algorithm>//#define maxstrlen 1000char S[1010];char t[11];int next[1010];using namespace std; void Next () {int i=0;int j=-1;next[0]=-1;int Len_t=strlen (t); while (i<len_t-1) {if (j==-1| | T[i]==t[j]) {i++;j++;next[i]=j;} else j=next[j];}} int KMP () {int i=-1;int j=-1;int Len_t=strlen (t); int Len_s=strlen (s); int count=0; Next (); while (i<len_s) {if (j==-1| | S[i]==t[j]) {i++;j++;} else{ J=next[j];
Copyright notice: This article Bo Master original article, may not have no consent reproduced!
Nyoj-binary String MATCHING-KMP algorithm