Nyoj-binary String MATCHING-BF algorithm

Source: Internet
Author: User

Binary String matching time limit: theMs | Memory Limit:65535KB Difficulty:3
Describe
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.
Sample input
 
 
Sample output
 
 
BF algorithm ***********/#include <cstdio> #include <cstring> #include <iostream> #include <cmath > #include <algorithm>using namespace std; #define Maxstrlen  1000int Main () {char S[maxstrlen+10];char t[11 ];int t;scanf ("%d", &t), while (t--) {scanf ("%s%s", t,s); int j=0,i=0;    int Len_s=strlen (s);    int Len_t=strlen (t);    int count=0;    while (i<=len_s)    {    if (S[i]==t[j])    {   ++i;   ++j;   if (j==len_t)   {       count++;      i=i-len_t+1;      j=0;    }      }    else    {   i=i-j+1;   j=0;    }       }       


Nyoj-binary String MATCHING-BF 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.