AtCoder Beginner Contest 174-D.Alter Altar (Thinking)

Link to the original question
Question meaning:
Given a string composed of R and W, you can exchange R and W each time, and ask how many times to exchange at least so that all R are in front of W.
Idea:
First of all, the number of R and W is fixed, so it must be all W at the end.
Assuming that there are a total of cnt Ws in the string, the number of Rs in the last cnt bit is the number of exchanges.
Code:

char s[maxn];
int main(){
    
    
	int n=read();
	cin>>s+1;
	int num=0,res=0;
	for(int i=1;i<=n;i++)
        if(s[i]=='W') num++;
    for(int i=n-num+1;i<=n;i++)
        if(s[i]=='R') res++;
    cout<<res<<endl;
	return 0;
}
 

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/110293143