[Cpp]
/*************************************** **********
Question:
Calculates the number of times the first string appears in the second string;
That is, how many times can a primary string match a child string;
Algorithm idea:
The KMP algorithm does not need to backtrack text strings in case of mismatching;
Instead, the obtained "partially matched" result is used to shift the pattern string right as far as possible to continue the comparison;
The pattern string does not necessarily move the position of a character to the right;
The right shift does not have to be re-matched from the start point of the mode string;
That is, the position where the mode string can be shifted to multiple characters at a time, and the position after the right shift can start from somewhere after the start of the mode string;
**************************************** **********/
# Include <iostream>
# Include <string>
# Include <cstring>
# Include <cstdio>
Using namespace std;
Const int N = 1000010; // the maximum length of a text string
Const int M = 10010; // 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
Int 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 characters of the text string
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;
}
/*************************************** **********
Question:
Calculates the number of times the first string appears in the second string;
That is, how many times can a primary string match a child string;
Algorithm idea:
The KMP algorithm does not need to backtrack text strings in case of mismatching;
Instead, the obtained "partially matched" result is used to shift the pattern string right as far as possible to continue the comparison;
The pattern string does not necessarily move the position of a character to the right;
The right shift does not have to be re-matched from the start point of the mode string;
That is, the position where the mode string can be shifted to multiple characters at a time, and the position after the right shift can start from somewhere after the start of the mode string;
**************************************** **********/
# Include <iostream>
# Include <string>
# Include <cstring>
# Include <cstdio>
Using namespace std;
Const int N = 1000010; // the maximum length of a text string
Const int M = 10010; // 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
Int 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 characters of the text string
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;
}