結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 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(); }
//---------------------------------------------------------------------------------------------------

typedef vector<int> vi;
typedef vector<vi> vvi;

bool visit(const vector<vi>& g, int v, vector<int>& order, vector<int>& color) {
    color[v] = 1;
    for (int u : g[v]) {
        if (color[u] == 2) continue;
        if (color[u] == 1) return false;
        if (!visit(g, u, order, color)) return false;
    }
    order.push_back(v); color[v] = 2;
    return true;
}

bool TopologicalSort(const vector<vi>& g, vector<int>& order) {
    int n = g.size();
    vector<int> color(n);
    for (int u = 0; u < n; u++) if (!color[u] && !visit(g, 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];
int cnt[26];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> A[i];
    cin >> M;
    rep(i, 0, M) cin >> B[i];

    rep(i, 0, N) for (char c : A[i]) cnt[c - 'A']++;
    rep(i, 0, 26) if (1 < cnt[i]) {
        printf("-1\n");
        return;
    }

    rep(i, 0, 26) cnt[i] = 0;
    rep(i, 0, M) for (char c : B[i]) cnt[c - 'A']++;
    rep(i, 0, 26) if (1 < cnt[i]) {
        printf("-1\n");
        return;
    }

    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';
            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';
            E[a][b] = 1;
        }
    }

    vvi EE(26);
    rep(i, 0, 26) {
        rep(j, 0, 26) if (E[i][j]) EE[i].push_back(j);
    }

    vector<int> ord;
    bool ok = TopologicalSort(EE, 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