编写算法,统计子串t在主串s中出现的次数。
输入格式:
首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据在第一行中输入主串s,在第二行中输入子串t,s和t中不包含空格。
输出格式:
对于每组测试,若子串t在主串s中出现,则输出t在s中的子串位置和出现总次数,否则输出“0 0”。引号不必输出。
输入样例:
2
abbbbcdebb
bb
abcde
bb
输出样例:
2 4
0 0
#include<stdio.h>
#include<string.h>
#define MAX 10010
int main()
{
int T;
char t[MAX],s[MAX],m[MAX];
scanf("%d",&T);
int i,j,k;
int flag=-1,lon,count=0;
for(i=0;i<T;i++){
scanf("%s",s);
scanf("%s",t);
lon=strlen(t);count=0;flag=-1;
for(j=0;s[j]!='\0';j++){
for(k=0;k<lon;k++){
m[k]=s[j+k];
}
m[lon]='\0';
if(strcmp(m,t)==0 && flag==-1){
flag=j+1;
}
if(strcmp(m,t)==0){
count++;
}
}
if(flag!=-1){
printf("%d %d\n",flag,count);
}else{
printf("0 0\n");
}
}
return 0;
}