Nyoj 5 binary string matching (KMP string matching)

Source: Internet
Author: User
Binary string matching time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
Description
Given two 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 '000000' while the pattern string a is '11', you shoshould output 3, because the pattern A appeared at the posit
Input
The first line consist only one integer N, indicates n cases follows. in each case, there are two lines, the first line gives the string a, length (a) <= 10, and the second line gives the string B, length (B) <= 1000. and it is guaranteed that B is always longer than.
Output
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of.
Sample Input
31110011101101011100100100100011010110100010101011 
Sample output
303 
Source
Network
Uploaded
Naonao

This question is too watery on Nanyang OJ; it is too watery to use simple algorithms directly, and there is too little data. Then we move the question to our own OJ, I have some extra data and tested it. The simple algorithm uses 3300 ms, and the KMP uses about ms for reference only. This shows the superiority of the KMP algorithm, I have been reading KMP for a long time in the last two days. I feel that some details are not in place. I still need to review and understand more. I want to see the details of the KMP algorithm on July's blog, read the data several times and analyze the data structure. You can review it later;
The following is a simple algorithm, violent;
The key to a simple algorithm lies in backtracking and Backtracking is well handled;
# Include <cstdio> # include <cstring> int main () {int N, count; char a [200], B [1200]; scanf ("% d ", & N); getchar (); While (n --) {COUNT = 0; int I = 0, j = 0, Len; scanf ("% s \ n % s ", a, B); Len = strlen (B); While (I <= Len) {if (a [J] = '\ 0') {count ++; I = I-j + 1; j = 0;} else if (a [J] = B [I]) {I ++; j ++ ;} else {I = I-j + 1; // The key lies in backtracking J = 0 ;}} printf ("% d \ n", count);} return 0 ;}
The following is the KMP algorithm, which must be enhanced to consolidate understanding;
There are a lot of unnecessary backtracing in the simple algorithm, so we have the KMP algorithm. We use a next array to store the location to be matched next time. What is hard to understand here is the next array, it also needs to understand the relationship between the suffix and the prefix, and obtain the next array by matching the relationship between the front and back of the string, without moving the location of the Main string; this saves time for backtracking;
# Include <cstdio> # include <cstring> int nextval [200]; void get_next (char a []) // obtain the next array; {int Len; int I = 0, j =-1; nextval [0] =-1; Len = strlen (a); While (I <= Len) {If (j =-1 | A [I] = A [J]) {++ I; ++ J; if (A [I] = A [J]) nextval [I] = nextval [J]; // replace all the Backtracking content with the next array; else nextval [I] = J;} else J = nextval [J];} int KMP (char a [], char B []) // main function of KMP {int I = 0, j = 0, Count = 0; int Lena, lenb; Lena = strlen (a); lenb = strlen (B ); get_next (a); While (I <= lenb) {If (j =-1 | A [J] = B [I]) {++ I; + + J;} else J = nextval [J]; If (j> = Lena) {count ++; j = nextval [J] ;}} return count ;} int main () {int N; char a [20], B [1200]; scanf ("% d", & N); While (n --) {scanf ("% s \ n % s", a, B); printf ("% d \ n", KMP (a, B) ;}return 0 ;}




Nyoj 5 binary string matching (KMP string matching)

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.