ACM Binary String Match

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void SubString(char sub[], char s[], int i, int m)
{
    int j;
    for(j=1; j<=m; j++)
        sub[j]=s[i++];
    sub[j]=NULL; 
}
int main()
{
    char s[1001], c[11], *sub=NULL;
    int num,m,n,i,count;
    scanf("%d", &num);
    while(num--)
    {
        scanf("%s", c+1);
        scanf("%s", s+1);
        m=strlen(c+1);
        n=strlen(s+1);
        sub=(char *)malloc((m+2)*sizeof(char));
        for(i=1,count=0; i<=n-m+1; i++)
        {
            SubString(sub,s,i,m);
            if(!strcmp(c+1,sub+1))
                count++;
        }
        printf("%d
",count);
    }
    return 1;
}
View Code


写的自我感觉不错!

原文地址:https://www.cnblogs.com/the-one/p/3554195.html