1. 程式人生 > 實用技巧 >小白進階之路-CF.Edu-91-B

小白進階之路-CF.Edu-91-B

題意:一段只包含'R,S,P'的字串,希望你輸出一段字串使得無論起始位置 pos 在哪,你勝率最大的組合。

錯誤思路:分字元種類情況分析。(錯誤原因,只是找到單次比較最大值,但不是全域性比較最大值)

正確思路:分字元數量情況分析。選最多的那個的對應字元。

#include <bits/stdc++.h>
using namespace  std;

void solve(){
    string s;cin >> s;
    int ok[5] = {0};
    for(char i : s){
        if(i == 'R'){
            ok[
1]++; }else if(i == 'S'){ ok[2]++; }else if(i == 'P'){ ok[3]++; } } int Max = max(ok[1],max(ok[2],ok[3])); char ans; if(Max == ok[1]){ ans = 'P'; }else if(Max == ok[2]){ ans = 'R'; }else if(Max == ok[3]){ ans
= 'S'; } int len = s.size(); while(len--){ printf("%c",ans); } printf("\n"); } int main() { ios::sync_with_stdio(false); int t ;cin >> t; while(t--) solve(); return 0; }