結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
|
提出日時 | 2017-05-28 21:31:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 971 bytes |
コンパイル時間 | 600 ms |
コンパイル使用メモリ | 75,428 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 15:17:14 |
合計ジャッジ時間 | 1,230 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> int main() { std::vector<int> next(26, -1); std::vector<int> prev(26, -1); int n; std::cin >> n; std::vector<bool> used(26); while (n--) { std::string s; std::cin >> s; for (char c : s) { used[c - 'A'] = true; } for (int j = 0; j + 1 < s.size(); j++) { next[s[j] - 'A'] = s[j + 1]; prev[s[j + 1] - 'A'] = s[j]; } } int m; std::cin >> m; while (m--) { std::string s; std::cin >> s; for (char c : s) { used[c - 'A'] = true; } for (int j = 0; j + 1 < s.size(); j++) { next[s[j] - 'A'] = s[j + 1]; prev[s[j + 1] - 'A'] = s[j]; } } int c = 0; for (int i = 0; i < 26; i++) { if (used[i] && prev[i] == -1) { c = i + 'A'; } } std::string s; while (c != -1) { s += c; c = next[c - 'A']; } if (s.size() < std::count(used.begin(), used.end(), true)) { std::cout << -1 << std::endl; } else { std::cout << s << std::endl; } }