#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
string text;
int cnt=0;
cin>>str;
getchar();
getline(cin,text);
string::size_type position = text.find(str);
while(text.find(str)!=string::npos)
{
cnt++;
text.replace(text.find(str),str.size(),"");
}
if(cnt==0)
printf("-1");
else
{
printf("%d %d",cnt,position);
}
return 0;
}
这种方法太简单了
这里我们可以回顾一下kmp算法的实现
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
string text;
int cnt=0;
cin>>str;
getchar();
getline(cin,text);
int position;
while(string::)
{
cnt++;
text.replace(text.find(str),str.size(),"");
}
if(cnt==0)
printf("-1");
else
{
printf("%d %d",cnt,position);
}
return 0;
}