結果

問題 No.517 壊れたアクセサリー
ユーザー finefine
提出日時 2017-05-28 22:15:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,099 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 170,176 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:17:15
合計ジャッジ時間 2,606 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 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 2 ms
4,348 KB
testcase_07 AC 1 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 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int root[26];
int nex[26];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<string> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	int m;
	cin >> m;
	vector<string> b(m);
	for (int i = 0; i < m; i++) cin >> b[i];
	for (int i = 0; i < 26; i++) {
		root[i] = -1;
		nex[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (root[a[i][0] - 'A'] == -1) root[a[i][0] - 'A'] = 1;
		for (int j = 0; j + 1 < a[i].length(); j++) {
			root[a[i][j + 1] - 'A'] = 0;
			nex[a[i][j] - 'A'] = a[i][j + 1] - 'A';
		}
	}
	for (int i = 0; i < m; i++) {
		if (root[b[i][0] - 'A'] == -1) root[b[i][0] - 'A'] = 1;
		for (int j = 0; j + 1 < b[i].length(); j++) {
			root[b[i][j + 1] - 'A'] = 0;
			nex[b[i][j] - 'A'] = b[i][j + 1] - 'A';
		}
	}
	int idx = -1;
	for (int i = 0; i < 26; i++) {
		if (root[i] == 1) {
			if (idx == -1) idx = i;
			else {
				idx = -1;
				break;
			}
		}
	}
	if (idx == -1) {
		cout << -1 << endl;
		return 0;
	}
	for (int i = idx; i != -1; i = nex[i]) {
		cout << (char)('A' + i);
	}
	cout << endl;
	return 0;
}
0