输出所有字串出现的位置
输入两个字符串A和B,输出B在A中出现的位置
输入
两行
第一行是一个含有空格的字符串
第二行是要查询的字串输出
字串的位置
样例输入
I love c++ c++
python
样例输出
-1
样例输入
I love c++ c++
c++
样例输出
8 12
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string a,b,c;
getline(cin,a);
getline(cin,b);
getline(cin,c);
a = a + "0";
int cnt = 0;
int pos = 0;
while(1)
{
int p = a.find(b,pos);
if(p==string::npos) break;
cout<<p<<" ";
cnt++;
pos = p + b.size();
}
if(cnt==0) cout<<-1;
return 0;
}
统计字串出现的次数
输入两个字符串A和B,输出B在A中出现的次数
输入
两行
第一行是一个含有空格的字符串
第二行是要查询的字串输出
字串的数量
样例输入
I love c++ c++
python
样例输出
0
样例输入
I love c++ c++
c++
样例输出
2
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string a,b;
getline(cin,a);
getline(cin,b);
int cnt = 0;
int pos = 0;
while(1)
{
if(a.find(b,pos)==string::npos) break;
cnt++;
pos = a.find(b,pos)+b.size();
}
cout<<cnt;
return 0;
}