結果

問題 No.517 壊れたアクセサリー
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-29 01:25:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,846 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 182,932 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:53:30
合計ジャッジ時間 2,401 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
struct TopologicalSort {
    vector<vector<int>> E; TopologicalSort(int N) { E.resize(N); }
    void add_edge(int a, int b) { E[a].push_back(b); }
    bool visit(int v, vector<int>& order, vector<int>& color) { color[v] = 1;
        for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false;
        if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; }
    bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n);
        for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false;
        reverse(order.begin(), order.end()); return true; } };
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/




int N; string A[30]; int M; string B[30]; int E[26][26];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> A[i];
    cin >> M;
    rep(i, 0, M) cin >> B[i];

    TopologicalSort ts(26);

    rep(i, 0, N) {
        if (A[i].length() == 1) continue;
        rep(j, 0, A[i].length() - 1) {
            int a = A[i][j] - 'A';
            int b = A[i][j + 1] - 'A';
            if (!E[a][b]) ts.add_edge(a, b);
            E[a][b] = 1;
        }
    }

    rep(i, 0, M) {
        if (B[i].length() == 1) continue;
        rep(j, 0, B[i].length() - 1) {
            int a = B[i][j] - 'A';
            int b = B[i][j + 1] - 'A';
            if (!E[a][b]) ts.add_edge(a, b);
            E[a][b] = 1;
        }
    }

    vector<int> ord;
    bool ok = ts.sort(ord);
    if (!ok) {
        printf("-1\n");
        return;
    }

    set<int> s;
    rep(i, 0, N) for (char c : A[i]) s.insert(c - 'A');
    rep(i, 0, M) for (char c : B[i]) s.insert(c - 'A');

    string ans = "";
    for (int i : ord) if (s.count(i)) ans += char('A' + i);

    ok = true;
    rep(i, 1, ans.size()) {
        int a = ans[i - 1] - 'A';
        int b = ans[i] - 'A';
        if (!E[a][b]) ok = false;
    }

    if (!ok) {
        printf("-1\n");
        return;
    }

    cout << ans << endl;
}
0