EOJ 3169. 字符串出现次数
题目内容:
定义函数 count(s,t),计算 t 在 s 中出现的次数。输入数据保证如有多个 t 的话,这些 t 不会相互重叠。
提示
例如:ab 在 abcdabcd 中出现 2 次,aa 在 xyaabb 中出现 1 次,xy 在 x 中出现 0 次。
思考:无
注意点:
1.查找母串中子串的位置s.find()函数。
附链接:(https://www.cnblogs.com/wkfvawl/p/9429128.html)
代码1:
#include<bits/stdc++.h>
using namespace std;
string s,t;
int pos,n;
int main()
{
cin>>s>>t;
while (s.find(t,pos)+1){
n++;
pos=s.find(t,pos)+1;
}
cout<<n<<endl;;
}
代码2:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,s1;
cin>>s>>s1;
int pos=-1,cnt=0;
while((pos=s.find(s1,pos+1))!=-1)
cnt++;
cout<<cnt<<endl;
}