跳到正文

个人记录

速通T3?不,是T3速通我

pengchenwei888约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月12日
更新
2026年6月14日
篇幅
约 2 分钟

版本

  1. v1当前

T3:

1.错误原因:

只写出了统计删除次数,不知如何写统计轮数。

2.错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
int  main(){
	int t,x=0;
    string a;
    cin>>t>>a;
		while(a.find("not")!=-1){
			int y=a.find("not");
			a.erase(y,3);
            x++;
		}	
	cout<<a<<endl<<x;
	return 0;
}

3.正确思路:

定义一个外层循环,记录轮数。内层则为一轮删除。

主要代码:

C++
while(a.find("not")!=-1){
		int y=0;
		while(a.find("not",y)!=-1){
			y=a.find("not",y);
			a.erase(y,3);
		}
		x++;
	}	

4.正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int  main(){
	int t,x=0;
    string a;
    cin>>t>>a;
	while(a.find("not")!=-1){//判断字符串是否还有not。
		int y=0;
		while(a.find("not",y)!=-1){//一轮删除,注意:要从y开始往后删除,仅删除目前已出现not。
			y=a.find("not",y);
			a.erase(y,3);
		}
		x++;    //一轮结束,加加。
	}	
	cout<<a<<endl<<x;
	return 0;
}
原文