跳到正文

个人记录

T3错误总结

chengpanpan约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年6月13日
收录
2026年6月13日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v1当前

错误原因

错误记录轮数,题目求的是轮数,不是删除 not的次数,同时也不能用两个指针来实现,因为在循环完一轮后还要从 0开始做下一轮。

错误代码

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int T;
	cin>>T;
	string s,t="not";
	cin>>s;
	int l=0,r=2,ans=0;
	for(int i=0;i<s.size();i++){
		string s1=s.substr(l,r-l+1);
		if(s1==t){
			s.erase(l,r-l);
			ans++;
			l=0,r=2;
		}
		l++,r++;
	}
	cout<<s<<"\n"<<ans;
	return 0;
}

正确思路

先写出删除一轮的代码,但可能无法正确记录轮数,比如这样的代码:

C++
while(s.find("not")!=-1){
		int k=s.find("not");
		s.erase(k,3);
}

因为这个代码会直接删除所有的 not,轮数错误导致 WA

所以可以记录一个 x,表示当前找到哪里了,让 find函数只往 x后面找

正确代码

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int T;
	cin>>T;
	string s;
	cin>>s;
	int ans=0;//记录轮数 
	while(s.find("not")!=-1) //循环轮数 
	{
		int x=0; 
		while(s.find("not",x)!=-1) ///做一轮删除 
		{
			x=s.find("not",x);//从哪里开始 
			s.erase(x,3);//删除 not 
		}
		ans++;//轮数 +1 
	}
	cout<<s<<endl<<ans;//输出结果 
	return 0;
}

6月13日断网二刷情况

WA,因为只改了 while循环的条件,没有改计算从哪里开始的那一行

错误代码

C++
#include<bits/stdc++.h>
using namespace std; 
int main(){
	int t;
	cin>>t;
	string s;
	cin>>s;
	int ans=0;
	while(s.find("not")!=-1){
		int x=0;
		while(s.find("not",x)!=-1){
			x=s.find("not");//←就是这里
			s.erase(x,3);
		}
		ans++;
	}
	cout<<s<<"\n"<<ans;
	return 0;
}

完结撒花~ ~

原文