Q老师与石头剪刀布(思路)

问题描述

每一个大人曾经都是一个小孩,Q老师 也一样。

为了回忆童年,Q老师 和 Monika 玩起了石头剪刀布的游戏,游戏一共 n 轮。无所不知的 Q老师 知道每一轮 Monika 的出招,然而作为限制, Q老师 在这 n 轮游戏中必须恰好出 a 次石头,b 次布和 c 次剪刀。

如果 Q老师 赢了 Monika n/2(上取整) 次,那么 Q老师就赢得了这场游戏,否则 Q老师 就输啦!

Q老师非常想赢,他想知道能否可以赢得这场游戏,如果可以的话,Q老师希望你能告诉他一种可以赢的出招顺序,任意一种都可以。

Input

第一行一个整数 t(1 ≤ t ≤ 100)表示测试数据组数。然后接下来的 t 组数据,每一组都有三个整数:

  • 第一行一个整数 n(1 ≤ n ≤ 100)
  • 第二行包含三个整数 a, b, c(0 ≤ a, b, c ≤ n)。保证 a+b+c=n
  • 第三行包含一个长度为 n 的字符串 s,字符串 s 由且仅由 ‘R’, ‘P’, ‘S’ 这三个字母组成。第 i 个字母 s[i] 表示 Monika 在第 i 轮的出招。字母 ‘R’ 表示石头,字母 ‘P’ 表示布,字母 ‘S’ 表示剪刀

Output

对于每组数据:

  • 如果 Q老师 不能赢,则在第一行输出 “NO”(不含引号)
  • 否则在第一行输出 “YES”(不含引号),在第二行输出 Q老师 的出招序列 t。要求 t 的长度为 n 且仅由 ‘R’, ‘P’, ‘S’ 这三个字母构成。t 中需要正好包含 a 个 ‘R’,b 个 ‘P’ 和 c 个 ‘S’

“YES”/"NO"是大小写不敏感的,但是 ‘R’, ‘P’, ‘S’ 是大小写敏感的。

Sample input

2
3
1 1 1
RPS
3
3 0 0
RPS

Sample output

YES
PSR
NO

解题思路

首先,Q老师在知道对手出拳顺序时,第一次遍历顺序,先从自己能出的招中找到可以胜利的,使之胜利,记录胜利次数。然后第二次遍历,将还剩下出招中找到可以平手的,使之平手。最后,剩下的出招都是必败的,出招。最终判断结果输出即可。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int t,n,a,b,c,win1;
string s;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>a>>b>>c>>s;//石头 布 剪刀
        win1=0;
        string stemp; stemp.clear();
        for (int i=0; i<n; i++){//先把能赢的列出来
            if(s[i]=='R' && b){
                stemp+='P'; b--; win1++; continue;
            }
            else if(s[i]=='P' && c){
                stemp+='S'; c--; win1++; continue;
            }
            else if(s[i]=='S' && a){
                stemp+='R'; a--; win1++; continue;
            }
            stemp+='#';//还没有比赛的
        }
        for (int i=0; i<n; i++){//然后是可以平局的
            if(stemp[i]=='#'){
                if(s[i]=='R' && a){
                    stemp[i]='R'; a--;
                }
                else if(s[i]=='P' && b){
                    stemp[i]='P'; b--;
                }
                else if(s[i]=='S' && c){
                    stemp[i]='S'; c--;
                }
            }
        }
        for (int i=0; i<n; i++){//最后是输的
            if(stemp[i]=='#'){
                if(s[i]=='R') stemp[i]='S';
                else if(s[i]=='P') stemp[i]='R';
                else stemp[i]='P';
            }
        }
        if(win1>=ceil(n*1.0/2.0))
            cout<<"YES"<<endl<<stemp<<endl;
        else cout<<"NO"<<endl;
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章