Pku3461 (oulipo) string matching-KMP Algorithm

Source: Internet
Author: User
/*************************************** * ********* 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 ;}

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.