結果

問題 No.3587 Too Good to Swap
コンテスト
ユーザー marc2825
提出日時 2026-05-28 13:29:29
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 1,667 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,167 ms
コンパイル使用メモリ 214,104 KB
実行使用メモリ 7,340 KB
最終ジャッジ日時 2026-07-10 20:54:55
合計ジャッジ時間 6,598 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 39 TLE * 1 -- * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

bool is_good(const string& s) {
    return s.find("good") != string::npos;
}

int rank_char(char c) {
    if (c == 'o') return 0;
    if (c == 'g') return 1;
    return 2; // d
}

vector<int> count_chars(const string& s) {
    vector<int> cnt(3, 0); // d, o, g
    for (char c : s) {
        if (c == 'd') cnt[0]++;
        else if (c == 'o') cnt[1]++;
        else cnt[2]++;
    }
    return cnt;
}

// 嘘の正規化:
// 左から見て、good を作らない隣接逆転を見つけたら即 swap。
// これをできなくなるまで繰り返す。
string fake_normalize(string s) {
    int n = (int)s.size();

    // 転倒数は高々 O(n^2) なので、適当に n^2 回程度回す。
    // ただし、嘘解法検証用なので安全に少し大きめ。
    for (int step = 0; step < n * n + 5 * n + 100; step++) {
        bool changed = false;

        for (int i = 0; i + 1 < n; i++) {
            if (rank_char(s[i]) <= rank_char(s[i + 1])) continue;

            string t = s;
            swap(t[i], t[i + 1]);

            if (is_good(t)) continue;

            s = t;
            changed = true;
            break;
        }

        if (!changed) break;
    }

    return s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int Q;
    cin >> Q;

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

        if (count_chars(S) != count_chars(T)) {
            cout << "No\n";
            continue;
        }

        string NS = fake_normalize(S);
        string NT = fake_normalize(T);

        cout << (NS == NT ? "Yes" : "No") << '\n';
    }

    return 0;
}
0