Binary string matching

Source: Internet
Author: User
Binary string matching

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 

 

 #include <iostream>#include <string>using namespace std;int main(){    int n;    cin>>n;    while(n--)    {        string strA;        string strB;        int count = 0;        cin>>strA;        cin>>strB;        int A = strA.size();        int B = strB.size();        for(int i=0; i<B; i++)        {            if(strB.substr(i, A) == strA)                count ++;        }        cout<<count<<endl;    }    return 0;}        

 

Binary 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.