Binary String matching time limit: theMs | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
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.
-
-
Sample input
-
-
-
-
Sample output
-
-
-
-
BF algorithm ***********/#include <cstdio> #include <cstring> #include <iostream> #include <cmath > #include <algorithm>using namespace std; #define Maxstrlen 1000int Main () {char S[maxstrlen+10];char t[11 ];int t;scanf ("%d", &t), while (t--) {scanf ("%s%s", t,s); int j=0,i=0; int Len_s=strlen (s); int Len_t=strlen (t); int count=0; while (i<=len_s) { if (S[i]==t[j]) { ++i; ++j; if (j==len_t) { count++; i=i-len_t+1; j=0; } } else { i=i-j+1; j=0; } }
Nyoj-binary String MATCHING-BF algorithm