NYOJ Binary String Matching

来源:互联网 发布:库里2016数据 编辑:程序博客网 时间:2024/06/08 16:43


Binary String Matching

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
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 ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
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 A.
输出
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.
样例输入
31110011101101011100100100100011010110100010101011 
样例输出
303 

思路:主要把B字符串拆分成长度和A相等的字符串,并存入另一个二维字符数组中。


#include<stdio.h>#include<string.h>int main(){    int t;    scanf("%d",&t);    while(t--)    {        char a[12],b[1100],c[1100][12];        scanf("%s %s",a,b);        int len,k,j,sum=0,i;        len=strlen(a);        strncpy(c[0],b,len);        //先把B数组中前len个字符存入c[0]中,下面会用到        k=1;        for(i=len; b[i]!='\0'; i++)   //B数组从len长度开始  c数组从c[1]开始存的        {            for(j=1; j<len; j++)             c[k][j-1]=c[k-1][j];   //从1开始,依次把二维数组上一个字符串存入下一个             c[k][j-1]=b[i];        //数组中,下一个字符数组是从零开始的,最后一个存b[i]             c[k][j]='\0';          //依次存入,             k++;        }        for(i=0;i<k;i++)            if(strcmp(c[i],a)==0)    //依次比较             sum++;             printf("%d\n",sum);    }    return 0;}


0 0
原创粉丝点击