結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
|
提出日時 | 2019-11-05 12:42:46 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,383 bytes |
コンパイル時間 | 1,370 ms |
コンパイル使用メモリ | 171,980 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-15 00:01:12 |
合計ジャッジ時間 | 2,148 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#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;vector<int> X[30];vector<int> Y[30];int main(){int N;cin >> N;bool fail = false;set<int> node;int last;REP(i,N){string S;cin >> S;last = S[0]-'A';node.insert(last);REPi(i,1,S.length()){int c = S[i]-'A';node.insert(c);if(X[last].size() == 0)X[last].push_back(c);else if(X[last][0] != c)fail = true;if(Y[c].size() == 0)Y[c].push_back(last);else if(Y[c][0] != last)fail = true;last = c;}}int M;cin >> M;REP(i,M){string S;cin >> S;last = S[0]-'A';node.insert(last);REPi(i,1,S.length()){int c = S[i]-'A';node.insert(c);if(X[last].size() == 0)X[last].push_back(c);else if(X[last][0] != c)fail = true;if(Y[c].size() == 0)Y[c].push_back(last);else if(Y[c][0] != last)fail = true;last = c;}}if(node.size() == 1){cout << (char)(last + 'A') << endl;return 0;}int head = -1;REP(i,26){if(X[i].size() == 1 && Y[i].size() == 0){if(head < 0)head = i;elsefail = true;}}if(head < 0) fail = true;string ans = "";int v = head;ans += (v + 'A');node.erase(v);while(1){if(X[v].size() == 0)break;if(X[v].size() != 1){fail = true;break;}else{v = X[v][0];node.erase(v);ans += (v + 'A');}}if(node.size() > 0)fail = true;if(fail){cout << -1 << endl;return 0;}cout << ans << endl;return 0;}