結果

問題 No.3587 Too Good to Swap
コンテスト
ユーザー marc2825
提出日時 2026-05-26 11:39:54
言語 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  
実行時間 -
コード長 2,863 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,395 ms
コンパイル使用メモリ 226,268 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-10 20:52:57
合計ジャッジ時間 6,454 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 48 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

bool wall_type(const string& X) {
    bool hasG = false, hasD = false;
    bool seenD = false;
    bool orderOK = true;

    // Remove all 'o'. The remaining string must be g...gd...d.
    for (char c : X) {
        if (c == 'o') continue;

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

    if (!hasG || !hasD || !orderOK) return false;

    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++;
    }

    return betweenO >= 3;
}

bool fast_judge(const string& S, const string& 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 if (c == 'g') cntS[2]++;
    }

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

    if (cntS != cntT) return false;

    return wall_type(S) == wall_type(T);
}

bool bfs_judge(const string& S, const string& T) {
    if (S.size() != T.size()) return false;
    if (is_good_string(S) || is_good_string(T)) return false;

    queue<string> que;
    set<string> seen;

    que.push(S);
    seen.insert(S);

    while (!que.empty()) {
        string cur = que.front();
        que.pop();

        if (cur == T) return true;

        int n = (int)cur.size();

        for (int i = 0; i + 1 < n; i++) {
            string nxt = cur;
            swap(nxt[i], nxt[i + 1]);

            if (is_good_string(nxt)) continue;
            if (seen.count(nxt)) continue;

            seen.insert(nxt);
            que.push(nxt);
        }
    }

    return false;
}

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

    int Q;
    cin >> Q;

    const int BFS_LIMIT = 10;

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

        bool ans = fast_judge(S, T);

        if ((int)S.size() <= BFS_LIMIT) {
            bool brute = bfs_judge(S, T);

            if (ans != brute) {
                cerr << "Mismatch found!\n";
                cerr << "S = " << S << '\n';
                cerr << "T = " << T << '\n';
                cerr << "fast  = " << (ans ? "Yes" : "No") << '\n';
                cerr << "brute = " << (brute ? "Yes" : "No") << '\n';
                return 1;
            }
        }

        cout << (ans ? "Yes" : "No") << '\n';
    }

    return 0;
}
0