結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
|
提出日時 | 2019-09-02 05:45:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,294 bytes |
コンパイル時間 | 1,227 ms |
コンパイル使用メモリ | 108,876 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-30 21:53:04 |
合計ジャッジ時間 | 2,110 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <stack>#include <algorithm>#include <cmath>#include <queue>#include <map>#include <set>#include <cstdlib>#include <bitset>#include <tuple>#include <assert.h>#include <fstream>#include <bitset>template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }const long long INF = 1LL << 60;const long long MOD = 1000000007LL;const long long MAX = 500000LL;using namespace std;typedef unsigned long long ull;typedef long long ll;int main(){ll N, M;cin >> N;vector<string> vs(N);for (ll i = 0; i < N; i++) {cin >> vs[i];}cin >> M;vector<string> vt(M);for (ll i = 0; i < M; i++) {cin >> vt[i];}set<char> st;for (ll i = 0; i < N; i++) {for (ll j = 0; j < vs[i].size(); j++) {st.insert(vs[i][j]);}}vector<ll> idg(26, 0);vector<vector<ll>> g(26);for (ll i = 0; i < N; i++) {for (ll j = 0; j < (ll)vs[i].size() - 1; j++) {g[vs[i][j] - 'A'].push_back(vs[i][j + 1] - 'A');idg[vs[i][j + 1] - 'A']++;}}for (ll i = 0; i < M; i++) {for (ll j = 0; j < (ll)vt[i].size() - 1; j++) {bool flag = true;for (ll k = 0; k < g[vt[i][j]-'A'].size(); k++) {if (g[vt[i][j] - 'A'][k] == vt[i][j + 1] - 'A') {flag = false;break;}}if (flag) {g[vt[i][j] - 'A'].push_back(vt[i][j + 1] - 'A');idg[vt[i][j + 1] - 'A']++;}}}queue<ll> q;vector<ll> topo;for (auto itr = st.begin(); itr != st.end(); itr++) {if (idg[*itr - 'A'] == 0) {q.push(*itr - 'A');topo.push_back(*itr - 'A');}}while (!q.empty()) {ll n = q.front();q.pop();for (ll nn : g[n]) {idg[nn]--;if (idg[nn] == 0) {q.push(nn);topo.push_back(nn);}}}if (topo.size() != st.size()) {puts("-1");return 0;}else {for (ll i = 0; i < (ll)topo.size() - 1; i++) {bool flag = false;for (ll x : g[topo[i]]) {if (x == topo[i + 1]) {flag = true;break;}}if (!flag) {puts("-1");return 0;}}for (ll i = 0; i < topo.size(); i++) {cout << (char)(topo[i] + 'A');}cout << endl;}return 0;}