PKU3461 (Oulipo) string matching-KMP Algorithm

Source: Internet
Author: User
Tags first string

[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;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.