题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4150
题目意思很简单,就是在字符串a中包含多少组不能重复的字符串b,直接暴力搜索;
在定义字符串这里wa了几下,唉,英语不行,(length<=10^6),a要开到百万位。
代码:
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000001],b[100001];
int s,i,j,p,count,len1,len2;
scanf("%d",&s);
while(s--)
{
count=0;
scanf("%s %s",a,b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<=len1-1;i++)
{
p=i;
for(j=0;j<=len2-1;j++)
{
if(a[p]==b[j]) {p++;}
else
break;
}
if(j==len2)
{
count++;
i=i+len2-1;
}
}
printf("%d\n",count);
}
return 0;
}
再贴一段错误代码,考试完在再想吧,先留在这里,免的忘了,考试要紧
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000001],b[100001];
int s,i,j,p,len1,len2,count;
scanf("%d",&s);
while(s--)
{
getchar();
scanf("%s %s",a,b);
len1=strlen(a);
len2=strlen(b);
p=0;count=0;
while(p<=len1)
{
i=p;j=0;
while(a[i]==b[j])
{i++;j++;}
if(j==len2)
{
count++;
continue;
}
else
p++;
}
printf("%d\n",count);
}
return 0;
}