結果

問題 No.517 壊れたアクセサリー
ユーザー 158b158b
提出日時 2017-05-28 23:15:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,488 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 74,740 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:43:11
合計ジャッジ時間 1,630 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};

const int alfaMax = 26;
string a[26], b[26];

struct data{
	int prev;
	bool use;
	int next;
}

data[alfaMax];

int search_first(void){
	for(int i=0; i<alfaMax; i++){
		if(data[i].use){
			if(data[i].prev == -1){
				return i;
			}
		}
	}
	return -1;
}

void make(string str){
	for(int i=0; i<str.length(); i++){
		data[str[i] - 'A'].use = true;
	}
	
	for(int i=0; i<str.length()-1; i++){
		data[str[i] - 'A'].next = str[i+1] - 'A';
		data[str[i+1] - 'A'].prev = str[i] - 'A';
	}
}

int main(){
	rep(i, alfaMax){
		data[i].prev = -1;
		data[i].use = false;
		data[i].next = -1;
	}
	
	int n, m;
	string str;
	cin >> n;
	rep(i,n){
		cin >> str;
		make(str);
	}
	cin >> m;
	rep(i,m){
		cin >> str;
		make(str);
	}
	
	int cnt = 0;
	rep(i,alfaMax){
		if(data[i].use){
			if(data[i].next == -1){
				cnt ++;
			}
		}
	}
	
	if(cnt != 1){
		cout << "-1" << endl;
	}else{
		string ans = "";
		int now = search_first();
		while(now != -1){
			ans += (now + 'A');
			now = data[now].next;
		}
		
		cout << ans << endl;
	}
	
	/*
	for(int i=0; i<alfaMax; i++){
		cout << (char)('A' + i) << ":next" << data[i].next << endl;
	}
	*/
	return 0;
}
0