题解:AT_joi2023_yo1a_c 3 つの箱 (Three Boxes)

· · 题解

因为 n \le 100 且只有 3 个箱子,直接模拟即可。如果 s_iL 就减 1,是 R 就加 1,在每次操作之后在更新答案。

code:

#include<bits/stdc++.h>
using namespace std;
int n,m=1,ans;//m : 目前小球的位置  ans : 答案
string s;
int main(){
    cin>>n>>s;
    for(int i=0;i<n;i++){
        if(s[i]=='L')x=max(x-1,1);//确保不超出范围
        else x=min(3,x+1);
        if(x==3)ans++;//更新答案
    }cout<<ans;
    return 0;
}