結果

問題 No.517 壊れたアクセサリー
ユーザー suika_psuika_p
提出日時 2019-11-03 13:54:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,226 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 170,608 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-13 01:47:22
合計ジャッジ時間 2,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill( (T*)array, (T*)(array+N), val ); }

int N, M;
vector<int> G[30];
vector<int> ans;
bool used[30];
int dp[30];

void dfs(int v) {
    if (used[v]) return;
    used[v] = true;
    for (int u : G[v]) {
        dfs(u);
    }
    ans.pb(v);
}

int dfs2(int v) {
    if (dp[v] != -1) return dp[v];
    int res = 1;
    for (int u : G[v]) {
        chmax(res, dfs2(u) + 1);
    }
    return dp[v] = res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> N;
    string s;
    rep(i, N) {
        cin >> s;
        int tmp = s.size();
        rep(j, s.size()-1) {
            G[s[j]-'A'].pb(s[j+1]-'A');
        }
    }

    cin >> M;
    int L = 0;
    rep(i, M) {
        cin >> s;
        int tmp = s.size();
        L += tmp;
        rep(j, s.size()-1) {
            G[s[j]-'A'].pb(s[j+1]-'A');
        }
    }

    rep(i, 26) {
        if (G[i].empty()) continue;
        dfs(i);
    }
    reverse(all(ans));

    Fill(dp, -1);
    int res = 0;
    rep(i, 26) {
        chmax(res, dfs2(i));
    }

    if (L == 1) {
        cout << s << endl;
        return 0;
    }
    if (L != res) {
        cout << -1 << endl;
        return 0;
    }
    rep(i, ans.size()) {
        cout << char(ans[i] + 'A');
    }
    cout << endl;
    
    return 0;
}
0