結果

問題 No.517 壊れたアクセサリー
ユーザー Manuel1024Manuel1024
提出日時 2021-09-20 03:03:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,644 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 88,916 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-15 02:05:35
合計ジャッジ時間 1,726 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <set>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<string> a(n);
    for(auto &p: a) cin >> p;
    int m;
    cin >> m;
    vector<string> b(m);
    for(auto &p: b) cin >> p;

    vector<set<int>> G(26);
    vector<int> is_exist(26);
    int len = 0;
    for(auto &p: a){
        len += p.length();
        for(auto &pp: p) is_exist[pp-'A'] = 1;
        for(int i = 1; i < p.length(); i++){
            G[p[i-1]-'A'].insert(p[i]-'A');
        }
    }
    for(auto &p: b){
        for(int i = 1; i < p.length(); i++){
            G[p[i-1]-'A'].insert(p[i]-'A');
        }
    }

    vector<int> in(26);
    for(int i = 0; i < 26; i++){
        for(auto &p: G[i]){
            in[p]++;
        }
    }

    vector<int> ans;
    queue<int> Q;
    for(int i = 0; i < 26; i++){
        if(is_exist[i] && in[i] == 0) Q.push(i);
    }

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

    while(Q.size()){
        int c = Q.front(); Q.pop();
        ans.push_back(c);
        for(auto &p: G[c]){
            in[p]--;
            if(in[p] == 0){
                Q.push(p);
            }
        }
    }
    /*
    for(int i = 0; i < 26; i++){
        cout << is_exist[i] << " : " << in[i] << " : " << G[i].size() << ": ";
        for(auto &p: G[i]) cout << p << " ";
        cout << endl;
    }
    for(auto &p: ans) cout << p << " ";
    */
    if(ans.size() != len) cout << -1 << endl;
    else{
        string st = "";
        for(auto &p: ans) st += (char)(p+'A');
        cout << st << endl;
    }
    return 0;
}
0