結果

問題 No.517 壊れたアクセサリー
ユーザー kura197kura197
提出日時 2019-11-05 13:06:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,814 bytes
コンパイル時間 1,376 ms
コンパイル使用メモリ 158,256 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 02:17:16
合計ジャッジ時間 2,319 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;

set<int> G[100];
set<int> R[100];

int main(){
    int N;
    cin >> N;
    int last;
    vector<int> node(26, -1);
    REP(i,N){
        string A;
        cin >> A;
        last = A[0] - 'A';
        chmax(node[last], 0);
        REPi(k,1,A.length()){
            int c = A[k] - 'A';
            R[c].insert(last);
            chmax(node[c], 1);
            G[last].insert(c);
            last = c;
        }
    }

    int M;
    cin >> M;
    REP(i,M){
        string A;
        cin >> A;
        last = A[0] - 'A';
        chmax(node[last], 0);
        REPi(k,1,A.length()){
            int c = A[k] - 'A';
            R[c].insert(last);
            chmax(node[c], 1);
            G[last].insert(c);
            last = c;
        }
    }

    vector<int> L;
    queue<int> S;

    REP(i,26){
        int n = node[i];
        if(n == 0){
            S.push(i);
        }
    }

    if(S.size() != 1){
        cout << -1 << endl;
        return 0;
    }

    while(!S.empty()){
        int n = S.front();
        S.pop();
        L.push_back(n);
        for(auto&& next : G[n]){
            if(R[next].find(n) == R[next].end()){
                cout << -1 << endl;
                return 0;
            }

            R[next].erase(n);
            if(R[next].size() == 0)
                S.push(next);
        }

    }


    REP(i,L.size()){
        printf("%c", L[i]+'A');
    }
    printf("\n");

    return 0;
}
0