/*************************************** * ********* The subject is as follows: evaluate the number of times that the first string appears in the second string, that is, how many times can the master string match the child string; Algorithm Idea: In the KMP algorithm, text strings do not need to be traced back when a mismatch occurs. Instead, the obtained "partially matched" result is used to shift the right distance of the pattern string as far as possible and continue the comparison; the mode string does not necessarily move the position of one character to the right; the right shift does not necessarily have to re-try the match from the start point of the mode string; that is, the mode string can shift the position of multiple characters to the right at a time, after the right shift, you can start matching from somewhere after the start point of the mode string; **************************************** * ********/# include <iostream> # include <string> # include <cstring> # include <cstdio> using namespace STD; const int n = 1000010; // the maximum length of the text string const int M = 10010; // the maximum length of the mode string int N; // the actual length of the text string int m; // the actual length of the mode string char T [N]; // text string char P [m]; // mode string in T next [m]; void getnext () // calculate the next function of the mode string P {Int J =-1; next [0] =-1; for (INT I = 1; I <m; I ++) {While (j> = 0 & P [J + 1]! = P [I]) J = next [J]; If (P [J + 1] = P [I]) J ++; next [I] = J ;}} int KMP () {Int J =-1; int sum = 0; For (INT I = 0; I <n; I ++) {While (j> = 0 & P [J + 1]! = T [I]) // when the next character in the mode string P does not match the text character, j = next [J]; if (P [J + 1] = T [I]) // when the next character in the mode string P matches the text character, J ++; if (J + 1 = m) // All characters of the mode string P match the Character sum ++;} return sum;} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin); int tcase; scanf ("% d ", & tcase); While (tcase --) {scanf ("% s", P); scanf ("% s", T); M = strlen (P ); N = strlen (t); getnext (); printf ("% d \ n", KMP () ;}return 0 ;}