結果

問題 No.517 壊れたアクセサリー
ユーザー mogurocker
提出日時 2017-10-16 02:04:16
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,214 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 160,236 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-17 18:53:59
合計ジャッジ時間 2,248 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i = 0; i < (n); i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;
typedef pair<int, int> P;

int n, m;
char nex[30];
string t[30];

int main(int argc, char const *argv[]) {
	ios_base::sync_with_stdio(false);
	cin >> n;
	fill(nex, nex+30, 'z');
	bool ok = true;
	int cnt = 0;
	FOR(i, n) {
		string s;
		cin >> s;
		cnt += s.size();
		FOR(j, s.size()-1) {
			if (nex[s[j]-'A'] == 'z') nex[s[j]-'A'] = s[j+1];
			else if (nex[s[j]-'A'] != s[j+1]) ok = false;
		}
	}
	cin >> m;
	FOR(i, m) {
		cin >> t[i];
		FOR(j, t[i].size()-1) {
			if (nex[t[i][j]-'A'] == 'z') nex[t[i][j]-'A'] = t[i][j+1];
			else if (nex[t[i][j]-'A'] != t[i][j+1]) ok = false;
		}
	}
	if (cnt == 1) {
		cout << t[0] << endl;
		return 0;
	}
	int cnt2 = 0;
	FOR(i, 26) {
		if (nex[i] != 'z') cnt2++;
	}
	if (!ok || cnt != cnt2+1) {
		cout << -1 << endl;
		return 0;
	}
	FOR(i, 26) {
		string ret = "";
		for (char c = (char)('A'+i); c != 'z'; c = nex[c-'A']) {
			ret += string(1, c);
		}
		if (ret.size() == cnt) {
			cout << ret << endl;
			return 0;
		}
	}
	return 0;
}
0