結果

問題 No.3323 岩井星式ジャンケン
コンテスト
ユーザー bolero
提出日時 2025-08-14 20:57:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,038 bytes
コンパイル時間 3,889 ms
コンパイル使用メモリ 293,468 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-31 20:27:20
合計ジャッジ時間 5,563 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main()
{
    int N, M;
    cin >> N >> M;
    vector<string> S(N);
    for (int i = 0; i < N; i++)
    {
        cin >> S[i];
    }

    map<char, char> win_hand = {{'G', 'P'}, {'C', 'G'}, {'P', 'C'}};

    // 負けた岩井星人
    vector<bool> is_loser(N, false);
    string ans;
    rep(i, M)
    {
        // 残っている岩井星人の手を集める。
        set<char> hands;
        rep(j, N)
        {
            if (is_loser[j])
            {
                continue;
            }

            hands.insert(S[j][i]);
        }

        switch (hands.size())
        {
        case 0: // 既に全ての岩井星人に勝利しているのでどの手を出してもよい。
            ans += "G";
            break;
        case 1: // 勝つ手を出す。
            ans += win_hand[*hands.begin()];
            break;
        case 2: // 負けない手を出す。
            if (hands.contains('G') && hands.contains('C'))
            {
                ans += 'G';
            }
            else if (hands.contains('C') && hands.contains('P'))
            {
                ans += 'C';
            }
            else if (hands.contains('P') && hands.contains('G'))
            {
                ans += 'P';
            }
            break;
        case 3: // 負けない手が存在しない。
            cout << -1 << endl;
            return 0;
        }

        // 負ける岩井星人を除外する。
        rep(j, N)
        {
            if (is_loser[j])
            {
                continue;
            }

            if (ans.back() == win_hand[S[j][i]])
            {
                is_loser[j] = true;
            }
        }
    }

    // 負けていない岩井星人が残っている。
    if (!all_of(is_loser.begin(), is_loser.end(), [](bool b)
                { return b; }))
    {
        cout << -1 << endl;
        return 0;
    }

    cout << ans << endl;

    return 0;
}
0