D 简单粗暴串
签到,字符串查找子串,注意不可重复。
用到字符串查找子串函数:find(string,int)
第一个参数为待查找子串,第二个参数为开始查找位置。
找到返回该子串首字母下标,未找到返回string::npos。
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s,c;
while(cin>>s>>c){
if(s=="#")break;
int ans=0;
int l=c.size();
int pos=0;
while((pos=s.find(c,pos))!=string::npos){
ans++;
pos+=l;//因为不可重复查找,所以下一个位置应该为子串首字符下标+子串长度
}
cout<<ans<<endl;
}
return 0;
}
京公网安备 11010502036488号