結果

問題 No.517 壊れたアクセサリー
ユーザー MisterMister
提出日時 2020-05-29 18:47:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,227 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 80,308 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-05 19:32:50
合計ジャッジ時間 1,647 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,388 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

void fail() {
    std::cout << "-1\n";
    std::exit(0);
}

void solve() {
    std::vector<int> to(26, -1);
    std::vector<int> in(26, 0);
    std::vector<bool> used(26, false);

    for (int q = 0; q < 2; ++q) {
        int n;
        std::cin >> n;
        while (n--) {
            std::string s;
            std::cin >> s;
            for (char c : s) used[c - 'A'] = true;

            int m = s.length();
            for (int i = 0; i + 1 < m; ++i) {
                int u = s[i] - 'A', v = s[i + 1] - 'A';
                to[u] = v;
                ++in[v];
            }
        }
    }

    int r = -1;
    for (int v = 0; v < 26; ++v) {
        if (!used[v]) continue;
        if (in[v] == 0) {
            if (r != -1) fail();
            r = v;
        }
    }
    if (r == -1) fail();

    std::string ans;
    while (r != -1) {
        ans.push_back('A' + r);
        used[r] = false;
        r = to[r];
    }

    if (std::any_of(used.begin(), used.end(), [](auto b) { return b; })) fail();
    std::cout << ans << "\n";
}

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

    solve();

    return 0;
}
0