結果

問題 No.3798 algo to goal
ユーザー marc2825
提出日時 2026-11-08 02:29:23
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 21 ms / 2,000 ms
+ 128µs
コード長 1,874 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,237 ms
コンパイル使用メモリ 220,720 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-11-13 22:03:13
合計ジャッジ時間 5,556 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

bool can_transform(const string& s, const string& t) {
    const int n = static_cast<int>(s.size());

    int count_s[2][26] = {};
    int count_t[2][26] = {};
    int position_t[2][26] = {};

    for (int i = 0; i < n; ++i) {
        const int parity = i & 1;
        const int a = s[i] - 'a';
        const int b = t[i] - 'a';

        ++count_s[parity][a];
        ++count_t[parity][b];
        position_t[parity][b] = i;
    }

    bool has_duplicate = false;

    for (int parity = 0; parity < 2; ++parity) {
        for (int c = 0; c < 26; ++c) {
            if (count_s[parity][c] != count_t[parity][c]) {
                return false;
            }

            if (count_s[parity][c] >= 2) {
                has_duplicate = true;
            }
        }
    }

    // 同じ位置偶奇の中に重複があれば、対応づけで偶奇を調整できる。
    if (has_duplicate) {
        return true;
    }

    // 重複なしなので、各文字の移動先が一意に決まる。
    // この分岐に入る場合、n <= 52。
    vector<int> permutation(n);

    for (int i = 0; i < n; ++i) {
        permutation[i] = position_t[i & 1][s[i] - 'a'];
    }

    // 固定点を含めて巡回の個数を数える。
    vector<char> visited(n, false);
    int cycle_count = 0;

    for (int i = 0; i < n; ++i) {
        if (visited[i]) {
            continue;
        }

        ++cycle_count;

        int v = i;
        while (!visited[v]) {
            visited[v] = true;
            v = permutation[v];
        }
    }

    return (n - cycle_count) % 2 == 0;
}

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

    int q;
    cin >> q;

    while (q--) {
        string s, t;
        cin >> s >> t;

        cout << (can_transform(s, t) ? "Yes" : "No") << '\n';
    }

    return 0;
}
0