題目連結: https://codeforces.com/problemset/problem/1380/B

# 內容

給一個只包含 “R”,”S“,”P“的字符串,分別代表石頭,剪刀、布,要求找到一個字符串,使得不管從哪個位置開始和給定字符串一一匹配之後獲勝的平均局數最多。

# 思路

直接尋找出現次數最多的,再全輸出克他的就好啦!!

# 程式碼

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int n;
    string s;
    cin>>n;
    while(n--){
        cin>>s;
        map<char, char> mp;
		mp['R'] = 'P';mp['P'] = 'S';mp['S'] = 'R';
		map<char, int> cnt;
		int Max = 0; char x;
		for(int i = 0; i < s.length(); ++i){
			cnt[s[i]]++;
			if(cnt[s[i]] > Max) Max = cnt[s[i]], x = s[i];
		}
        for(int i = 0;i<s.size();i++)cout<<mp[x];
        cout<<endl;
   }
}
更新於 閱讀次數

用實際行動犒賞爆肝的我😀

Zrn Ye LinePay

LinePay