結果
| 問題 |
No.517 壊れたアクセサリー
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-11-05 13:06:20 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,814 bytes |
| コンパイル時間 | 1,527 ms |
| コンパイル使用メモリ | 174,220 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-15 00:01:18 |
| 合計ジャッジ時間 | 2,217 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
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;
}