結果

問題 No.517 壊れたアクセサリー
ユーザー square1001square1001
提出日時 2017-08-20 12:36:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 826 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 164,108 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 18:10:53
合計ジャッジ時間 2,257 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int N, M, e, sum, deg[26]; bool ap[26], g[26][26]; string s, ret;
void dfs(int pos) {
	ret += pos + 65;
	for(int i = 0; i < 26; i++) {
		if(g[pos][i]) dfs(i);
	}
}
int main() {
	cin >> N;
	for(int i = 0; i < N; i++) {
		cin >> s; sum += s.size();
		for(int j = 0; j < s.size(); j++) ap[s[j] - 65] = true;
		for(int j = 1; j < s.size(); j++) {
			int a = s[j - 1] - 65, b = s[j] - 65;
			if(!g[a][b]) g[a][b] = true, deg[b]++, e++;
		}
	}
	cin >> M;
	for(int i = 0; i < M; i++) {
		cin >> s;
		for(int j = 1; j < s.size(); j++) {
			int a = s[j - 1] - 65, b = s[j] - 65;
			if(!g[a][b]) g[a][b] = true, deg[b]++, e++;
		}
	}
	if(e + 1 == sum) {
		for(int i = 0; i < 26; i++) {
			if(deg[i] == 0 && ap[i]) dfs(i);
		}
		cout << ret << endl;
	}
	else cout << -1 << endl;
	return 0;
}
0