結果
| 問題 |
No.517 壊れたアクセサリー
|
| コンテスト | |
| ユーザー |
xuzijian629
|
| 提出日時 | 2018-10-10 17:55:33 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,705 bytes |
| コンパイル時間 | 2,004 ms |
| コンパイル使用メモリ | 178,916 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-12 17:18:21 |
| 合計ジャッジ時間 | 2,959 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
map<char, int> toint;
map<int, char> tochar;
int main() {
int n;
cin >> n;
vector<string> vs;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (char c: s) {
assert(toint.count(c) == 0);
toint[c] = cnt++;
tochar[toint[c]] = c;
}
vs.push_back(s);
}
vector<vector<int>> adj(cnt, vector<int>(cnt));
for (string& s: vs) {
if (s.size() > 1) {
for (int i = 1; i < s.size(); i++) {
adj[toint[s[i - 1]]][toint[s[i]]] = 1;
}
}
}
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s.size() > 1) {
for (int i = 1; i < s.size(); i++) {
assert(toint.count(s[i - 1]) && toint.count(s[i]));
adj[toint[s[i - 1]]][toint[s[i]]] = 1;
}
}
}
for (int i = 0; i < cnt; i++) {
int s = 0;
for (int j = 0; j < cnt; j++) {
s += adj[i][j];
}
assert(s <= 1);
}
for (int i = 0; i < cnt; i++) {
string ans = "";
int crt = i;
while (1) {
ans += tochar[crt];
bool hasnext = false;
for (int j = 0; j < cnt; j++) {
if (adj[crt][j]) {
crt = j;
hasnext = true;
break;
}
}
if (!hasnext) {
break;
}
}
if (ans.size() == cnt) {
cout << ans << endl;
return 0;
}
}
cout << -1 << endl;
}
xuzijian629