結果
| 問題 |
No.517 壊れたアクセサリー
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-05-29 01:25:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,846 bytes |
| コンパイル時間 | 1,921 ms |
| コンパイル使用メモリ | 182,756 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 16:07:53 |
| 合計ジャッジ時間 | 2,810 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
struct TopologicalSort {
vector<vector<int>> E; TopologicalSort(int N) { E.resize(N); }
void add_edge(int a, int b) { E[a].push_back(b); }
bool visit(int v, vector<int>& order, vector<int>& color) { color[v] = 1;
for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false;
if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; }
bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n);
for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false;
reverse(order.begin(), order.end()); return true; } };
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N; string A[30]; int M; string B[30]; int E[26][26];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N;
rep(i, 0, N) cin >> A[i];
cin >> M;
rep(i, 0, M) cin >> B[i];
TopologicalSort ts(26);
rep(i, 0, N) {
if (A[i].length() == 1) continue;
rep(j, 0, A[i].length() - 1) {
int a = A[i][j] - 'A';
int b = A[i][j + 1] - 'A';
if (!E[a][b]) ts.add_edge(a, b);
E[a][b] = 1;
}
}
rep(i, 0, M) {
if (B[i].length() == 1) continue;
rep(j, 0, B[i].length() - 1) {
int a = B[i][j] - 'A';
int b = B[i][j + 1] - 'A';
if (!E[a][b]) ts.add_edge(a, b);
E[a][b] = 1;
}
}
vector<int> ord;
bool ok = ts.sort(ord);
if (!ok) {
printf("-1\n");
return;
}
set<int> s;
rep(i, 0, N) for (char c : A[i]) s.insert(c - 'A');
rep(i, 0, M) for (char c : B[i]) s.insert(c - 'A');
string ans = "";
for (int i : ord) if (s.count(i)) ans += char('A' + i);
ok = true;
rep(i, 1, ans.size()) {
int a = ans[i - 1] - 'A';
int b = ans[i] - 'A';
if (!E[a][b]) ok = false;
}
if (!ok) {
printf("-1\n");
return;
}
cout << ans << endl;
}
はまやんはまやん