#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,cot=0,cot2=0;
cin>>n;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s.find("swust")!=-1)
{
cot++;
int a = s.find("swust");
s.erase(a,a+4);
}
}
cout<<cot<<endl;
}
| 表头 | 表头 | 表头 | 表头 |
| ------ | ------ | ------ | ------ |
| 单元格 | 单元格 | 单元格 | 单元格 |
| 单元格 | 单元格 | 单元格 | 单元格 |
| 单元格 | 单元格 | 单元格 | 单元格 |
![](https://profile-avatar.csdnimg.cn/default.jpg!4)
请问我哪儿错了,有空请看一下
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 语言-逆行者 2023-04-14 23:44关注
你的代码中有一个问题。在你的循环中,你使用了s.find("swust")来查找子串"swust",但是你没有更新查找的起始位置。这意味着,如果字符串中包含多个"swust"子串,你的代码只会找到第一个。
此外,你使用了s.erase(a,a+4)来删除找到的子串。但是s.erase(a,a+4)并不会删除从位置a开始的5个字符,而是删除从位置a开始的4个字符。正确的写法应该是s.erase(a,5)。
下面是修改后的代码:#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,cot=0,cot2=0; cin>>n; string s; cin>>s; size_t pos = 0; while ((pos = s.find("swust", pos)) != string::npos) { cot++; pos += 5; } cout<<cot<<endl; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥50 这Mac系统提示虚拟内存不足,怎么解决
- ¥15 Rs232电路无法收发数据,求帮助
- ¥15 百度cookie扫码登录器
- ¥15 微机原理汇编语言debug调试实验
- ¥23 matlab可以把相图转换为庞加莱映射吗
- ¥20 有偿,学生成绩信息管理系统
- ¥15 Arduino电机和openmv连接异常
- ¥15 Arcgis河网分级报错
- ¥200 java+appium2.1+idea
- ¥20 请帮我做一个EXE的去重TXT文本