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
31110011101101011100100100100011010110100010101011
Sample output
60V
/* The BF algorithm originally used, and later changed into the KMP algorithm, the first contact KMP algorithm, still not too understanding. On code */#include <stdio.h> #include <string.h>void getnext (char *p,int *next) {int j = 0, k = -1;*next = -1; while (* (p+j)! = ' \ 0 ') { if (k==-1| | * (P+J) ==* (p+k)) { j++; k++; * (NEXT+J) = k; } else k=* (next+k); }} Int&nbSp Kmpmatch (CHAR&NBSP;*A1,&NBSP;CHAR&NBSP;*A2) {int next[11];int i,j,num;i=j=num=0; GetNext (A1,next); while (* (a1+i)! = ' &&* (a2+j)! = ' + ') {if ( -1 == i | | * (a1+i) ==* (a2+j)) {++i;++j;} Else{i=next[i];} if (* (a1+i) = = ' + ') {++num;j=j-i+1;i=0;}} Return num;} Int main () {int n;char a1[11], a2[1000];scanf ("%d", &n), GetChar (); while (n--) {scanf ("%s", A1); GetChar (); scanf ("%s", A2), GetChar ();p rintf ("%d\n", Kmpmatch (A1,A2));} return 0;}
Binary String Matching