結果

問題 No.517 壊れたアクセサリー
ユーザー treeonetreeone
提出日時 2017-05-28 22:08:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,137 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 161,772 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:13:42
合計ジャッジ時間 2,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

signed main(){
    int n;
    cin >> n;
    int G[26][26] = {};
    int cnt = 0;
    string s;
    rep(i, 0, n){
        cin >> s;
        cnt += s.size();
        rep(j, 0, s.size() - 1){
            G[s[j] - 'A'][s[j + 1] - 'A'] = 1;
            // G[s[j + 1] - 'a']. push_back(s[j] - 'a');
        }
    }
    int m;
    cin >> m;
    rep(i, 0, m){
        cin >> s;
        rep(j, 0, s.size() - 1){
            G[s[j] - 'A'][s[j + 1] - 'A'] = 1;            
            // G[s[j + 1] - 'a']. push_back(s[j] - 'a');
        }
    }
    if(cnt == 1){
        cout << s << endl;
        return 0;
    }
    int st = -1;
    bool f = true;
    rep(i, 0, 26){
        int tmp = 0;
        rep(j, 0, 26){
            if(G[i][j]){
                tmp++;
            }
        }
        if(tmp > 1) f = false;
        int tmp2 = 0;
        rep(j, 0, 26){
            if(G[j][i]){
                tmp2++;
            }
        }
        if(tmp2 > 1) f = false;
        if(tmp == 1 && tmp2 == 0){
            if(st == -1) st = i;
            else f = false;
        }
    }
    if(st == -1) f = false;
    // cout << f << endl;
    // cout << "  ";
    // rep(i, 0, 26){
    //     cout << (char)('A' + i) << " ";
    // }
    // cout << endl;
    // rep(i, 0 ,26){
    //     cout << (char)('A' + i) << " ";
    //     rep(j, 0, 26){
    //         cout << G[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    string ans = "";
    int now = st;
    ans += (char)('A' + st);
    // cout << ans << endl;
    while(1){
        bool upd = false;
        rep(i, 0, 26){
            if(G[now][i]){
                upd = true;
                now = i;
                ans += ('A' + now);
            }
        }
        if(!upd) break;
    }
    if(ans.size() != cnt) f = false;
    if(!f) cout << -1 << endl;
    else cout << ans << endl; 
}
0