Nyoj-binary String MATCHING-KMP algorithm

Source: Internet
Author: User

Binary String matching time limit: theMs | Memory Limit:65535KB Difficulty:3
Descriptive narrative
Given 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 ' 1001110110 ' and the pattern string A is ' one ', you should output 3, because the patter N A appeared at the posit

Input
The first line consist only one
integer n, indicates n cases follows. In each case, there is lines, the first line gives the string A, Length (a) <=, and the second line gives the S Tring B, Length (b) <= 1000. And it is guaranteed this B is always longer than A.
Output
for each case, the output a single line consist a single integer, tells how the many times do B appears as a substring of a.
Example input
 
 
Example output
 
 
/******KMP algorithm *********/#include <cstdio> #include <cstring> #include <iostream> #include <cmath > #include <algorithm>//#define maxstrlen  1000char S[1010];char t[11];int next[1010];using namespace std; void Next () {int i=0;int j=-1;next[0]=-1;int Len_t=strlen (t); while (i<len_t-1) {if (j==-1| | T[i]==t[j]) {i++;j++;next[i]=j;} else    j=next[j];}} int KMP () {int i=-1;int j=-1;int Len_t=strlen (t); int Len_s=strlen (s); int count=0; Next (); while (i<len_s) {if (j==-1| | S[i]==t[j]) {i++;j++;} else{       J=next[j];    


Copyright notice: This article Bo Master original article, may not have no consent reproduced!

Nyoj-binary String MATCHING-KMP algorithm

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.