結果

問題 No.3587 Too Good to Swap
コンテスト
ユーザー marc2825
提出日時 2026-05-26 11:36:24
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,936 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,960 ms
コンパイル使用メモリ 336,584 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-10 20:52:47
合計ジャッジ時間 4,111 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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


int main() {
    int Q;
    cin >> Q;

    while (Q--) {
        string S, T;
        cin >> S >> T;

        vector<int> cntS(3, 0), cntT(3, 0); // d, o, g

        for (char c : S) {
            if (c == 'd') cntS[0]++;
            else if (c == 'o') cntS[1]++;
            else cntS[2]++;
        }

        for (char c : T) {
            if (c == 'd') cntT[0]++;
            else if (c == 'o') cntT[1]++;
            else cntT[2]++;
        }

        if (cntS != cntT) {
            cout << "No\n";
            continue;
        }
        

        bool wallS = false, wallT = false;

        for (int id = 0; id < 2; id++) {
            string X = (id == 0 ? S : T);

            bool hasG = false, hasD = false;
            bool seenD = false;
            bool orderOK = true;

            for (char c : X) {
                if (c == 'o') continue;

                if (c == 'd') {
                    hasD = true;
                    seenD = true;
                } else { // c == 'g'
                    hasG = true;
                    if (seenD) orderOK = false;
                }
            }

            bool wall = false;

            if (hasG && hasD && orderOK) {
                int lastG = -1;
                int firstD = (int)X.size();

                for (int i = 0; i < (int)X.size(); i++) {
                    if (X[i] == 'g') lastG = i;
                    if (X[i] == 'd') {
                        firstD = i;
                        break;
                    }
                }

                int betweenO = 0;
                for (int i = lastG + 1; i < firstD; i++) {
                    if (X[i] == 'o') betweenO++;
                }

                wall = (betweenO >= 3);
            }

            if (id == 0) wallS = wall;
            else wallT = wall;
        }

        cout << (wallS == wallT ? "Yes" : "No") << '\n';
    }

    return 0;
}
0