結果

問題 No.517 壊れたアクセサリー
ユーザー pekempeypekempey
提出日時 2017-05-28 21:31:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 75,432 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:03:03
合計ジャッジ時間 1,470 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
	}
}
0