编写算法,统计子串t在主串s中出现的次数。
输入格式:
首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据在第一行中输入主串s,在第二行中输入子串t,s和t中不包含空格。
输出格式:
对于每组测试,若子串t在主串s中出现,则输出t在s中的子串位置和出现总次数,否则输出“0 0”。引号不必输出。
输入样例:
2
abbbbcdebb
bb
abcde
bb
输出样例:
2 4
0 0
#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
char s[10000],t[100],a[100];
cin>>s>>t;
int ls=strlen(t),con=0,flag=-1;
for(int i=0;s[i]!='\0';i++){
for(int j=0;j<ls;j++){
a[j]=s[j+i];
}
a[ls]='\0';
if(strcmp(a,t)==0&&flag==-1)
flag=i+1;
if(strcmp(a,t)==0)
con++;
}
if(flag!=-1)cout<<flag<<" "<<con<<endl;
else cout<<"0 0"<<endl;
}
return 0;
}