题意:
给定字符串s s只包括两种字符’R’和’W’ 'W’不能出现在’R’的左边
现在给出两种操作
1:交换两个字符
2.把一个字符更改为’R’或’W’
问你至少多少次操作满足 'W’不能出现在’R’的左边
解析:
双指针 l=1,r=n;
如果s[l]==‘W’ ,s[r]=‘R’ 就直接交换 直到满足条件为止
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+1000;
char s[N];
int n;
int main()
{
cin>>n;
cin>>(s+1);
int l=1,r=n;
int cnt=0;
while(l<r)
{
while(s[l]=='R') l++;
while(s[r]=='W') r--;
if(l<r) swap(s[l],s[r]),cnt++;
}
cout<<cnt<<endl;
return 0;
}