Binary 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 code:
# Include <stdio. h> # include <string. h> # include <stdlib. h> char s [1000], t [10]; void index (char T [10], char s [1000]) // data structure-based matching BP algorithm {int I = 0, j = 0; int COUNT = 0; while (I <strlen (s )) {If (s [I] = T [J]) {I ++; j ++;} else {I = I-j + 1; j = 0 ;} if (j> = strlen (t) {count ++; I = I-j + 1; j = 0 ;}} printf ("% d \ n ", count) ;}int main () {int N; scanf ("% d", & N); // getchar (); While (n --) {scanf ("% S % s", t, s); index (t, s);} system ("pause"); 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.