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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章