Binary string matching time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
-
Description
Given two 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 '000000' while the pattern string a is '11', you shoshould output 3, because the pattern A appeared at the posit
Input
The first line consist only one integer N, indicates n cases follows. In each case, there are two lines, the first line gives the string a, length (a) <= 10, and
The second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than.
Output
-
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of.
-
Sample Input
-
31110011101101011100100100100011010110100010101011
-
Sample output
-
303
-
Source code:
-
# Include <stdio. h> # include <string. h> # include <stdlib. h> char s [1000], t [10]; void index (char T [10], char s [1000]) // data structure-based matching BP algorithm {int I = 0, j = 0; int COUNT = 0; while (I <strlen (s )) {If (s [I] = T [J]) {I ++; j ++;} else {I = I-j + 1; j = 0 ;} if (j> = strlen (t) {count ++; I = I-j + 1; j = 0 ;}} printf ("% d \ n ", count) ;}int main () {int N; scanf ("% d", & N); // getchar (); While (n --) {scanf ("% S % s", t, s); index (t, s);} system ("pause"); Return 0 ;}
Binary string matching