結果

問題 No.517 壊れたアクセサリー
ユーザー hexagon_emilhexagon_emil
提出日時 2018-04-16 00:40:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,390 bytes
コンパイル時間 1,469 ms
コンパイル使用メモリ 164,008 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 09:19:42
合計ジャッジ時間 2,346 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

///////////////////////////////////////////
const int INF = 100000000;
using ll = long long int;
using vi = vector<int>;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rep(i,N) for(int i=0;i<N;i++)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return 1; } return 0; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return 1; } return 0; }
///////////////////////////////////////////


struct unweighted_edge{//重み無し有向グラフ
    char to;
    unweighted_edge(char t){to=t;};
};
typedef vector<vector<unweighted_edge>> graph;

using ue = unweighted_edge;

int main(){
    ll n,m;
   // int box[100000];
    cin>>n;
    string ss;
    map<char,int> s;
    vector<string> k,k2;
    graph G(10000);
    int l=0;
    rep(i,n){
        cin>>ss;
        k.push_back(ss);
        rep(j,ss.size()){
            s.insert(make_pair(ss[j],l));
            l++;
        }
    }
    rep(i,k.size()){
        rep(j,k[i].size()-1){
            ue e(k[i][j+1]);
            G[s[k[i][j]]].push_back(e);
        }
    }
    cin>>m;
    rep(i,m){
        cin>>ss;
        k2.pb(ss);
        rep(j,ss.size()-1){
            if(G[s[ss[j]]].size()==0){ ue e(ss[j+1]); G[s[ss[j]]].push_back(e); }
        }
    }
    bool feag=false;
    rep(i,k.size()){
        if(find(all(k2),k[i])!=k2.end()){
            feag = true;
        }
    }
    if(feag){
        if(k2.size()==1){
            cout<<k2[0]<<endl;
        }else{
            cout<<-1<<endl;
        }
    }else {
        int num = 0;
        int now;
        string ans;
        bool flag = false;
        rep(i, k.size()) {
            now = i;
            ans += k[now][0];
            now = s[k[now][0]];
            num++;
            while (1) {
                if (num > l) { break; }
                if (G[now].size() == 0)break;
                ans += G[now][0].to;
                //cout << ans << endl;
                now = s[G[now][0].to];
                num++;
            }
            if (num == l) {
                flag = true;
                break;
            } else {
                num = 0;
                ans.clear();
            }
        }
        if (flag == true) {
            cout  << ans << endl;
        } else {
            cout << -1 << endl;
        }
    }
    return 0;
}
0