結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 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;

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N,M;
bool edge [26] [26];
bool have [26];

int main()
{
	scanf("%d",&N);
	vector<string> S(N);
	vector<int> indeg(26),outdeg(26);
	FOR(i,0,N){
		cin >> S [i];
		FOR(j,0,S [i].size()) have [S [i] [j] - 'A'] = true;
		FOR(j,1,S [i].size()){
			int u = S [i] [j - 1] - 'A',v = S [i] [j] - 'A';
			if(edge [u] [v] == false){
				edge [u] [v] = true;
				outdeg [u]++;
				indeg [v]++;
			}
		}
	}
	scanf("%d",&M);
	vector<string> T(M);
	FOR(i,0,M){
		cin >> T [i];
		FOR(j,1,T [i].size()){
			int u = T [i] [j - 1] - 'A',v = T [i] [j] - 'A';
			if(edge [u] [v] == false){
				edge [u] [v] = true;
				outdeg [u]++;
				indeg [v]++;
			}
		}
	}

	FOR(i,0,26) if(indeg [i] >= 2 || outdeg [i] >= 2){
		puts("-1");
		return 0;
	}

	int K = accumulate(have,have + 26,0);
	string ans;
	while(ans.size() < K){
		bool flag = true;
		vector<int> v;
		FOR(i,0,26) if(have [i] && indeg [i] == 0){
			v.push_back(i);
			have [i] = false;
		}
		if(v.size() == 1){
			ans += char('A' + v.front());
			FOR(i,0,26) if(edge [v.front()] [i]){
				indeg [i]--;
			}
		}
		else{
			puts("-1");
			return 0;
		}
	}
	cout << ans << endl;

	return 0;
}
0