結果

問題 No.517 壊れたアクセサリー
ユーザー 👑 jupirojupiro
提出日時 2019-09-02 05:45:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,294 bytes
コンパイル時間 1,306 ms
コンパイル使用メモリ 107,256 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 18:08:31
合計ジャッジ時間 2,268 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <fstream>
#include <bitset>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long INF = 1LL << 60;
const long long MOD = 1000000007LL;
const long long MAX = 500000LL;
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;


int main()
{
	ll N, M;	
	cin >> N;
	vector<string> vs(N);
	for (ll i = 0; i < N; i++) {
		cin >> vs[i];
	}
	cin >> M;
	vector<string> vt(M);
	for (ll i = 0; i < M; i++) {
		cin >> vt[i];
	}
	set<char> st;
	for (ll i = 0; i < N; i++) {
		for (ll j = 0; j < vs[i].size(); j++) {
			st.insert(vs[i][j]);
		}
	}
	vector<ll> idg(26, 0);
	vector<vector<ll>> g(26);
	for (ll i = 0; i < N; i++) {
		for (ll j = 0; j < (ll)vs[i].size() - 1; j++) {
			g[vs[i][j] - 'A'].push_back(vs[i][j + 1] - 'A');
			idg[vs[i][j + 1] - 'A']++;
		}
	}

	for (ll i = 0; i < M; i++) {
		for (ll j = 0; j < (ll)vt[i].size() - 1; j++) {
			bool flag = true;
			for (ll k = 0; k < g[vt[i][j]-'A'].size(); k++) {
				if (g[vt[i][j] - 'A'][k] == vt[i][j + 1] - 'A') {
					flag = false;
					break;
				}
			}
			if (flag) {
				g[vt[i][j] - 'A'].push_back(vt[i][j + 1] - 'A');
				idg[vt[i][j + 1] - 'A']++;
			}
		}
	}

	queue<ll> q;
	vector<ll> topo;
	for (auto itr = st.begin(); itr != st.end(); itr++) {
		if (idg[*itr - 'A'] == 0) {
			q.push(*itr - 'A');
			topo.push_back(*itr - 'A');
		}
	}
	while (!q.empty()) {
		ll n = q.front();
		q.pop();
		for (ll nn : g[n]) {
			idg[nn]--;
			if (idg[nn] == 0) {
				q.push(nn);
				topo.push_back(nn);
			}
		}
	}

	if (topo.size() != st.size()) {
		puts("-1");
		return 0;
	}
	else {
		for (ll i = 0; i < (ll)topo.size() - 1; i++) {
			bool flag = false;
			for (ll x : g[topo[i]]) {
				if (x == topo[i + 1]) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				puts("-1");
				return 0;
			}
		}
		for (ll i = 0; i < topo.size(); i++) {
			cout << (char)(topo[i] + 'A');
		}
		cout << endl;
	}
	return 0;
}
0