题解:AT_abc394_c [ABC394C] Debug

根据题目描述,对于每一个 WA 要替换成 AC,如果我们从前往后遍历会出现问题,比如 WWA,从前往后遍历只会把结尾的 WA 换成 AC,也就会变成 WA,明显不正确。

所以从后往前遍历,便可以很好的解决这类问题。最终复杂度为 \(O(n)\)

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
	getline(cin,s);
	for(int i=s.size()-1;i>=0;i--){
		if(s[i]=='A'&&s[max(i-1,0)]=='W'){
			s[i]='C',s[i-1]='A';
		}
	}
	cout<<s;
	return 0;
} 
posted @ 2025-09-10 21:41  一班的hoko  阅读(5)  评论(0)    收藏  举报