結果
| 問題 |
No.517 壊れたアクセサリー
|
| コンテスト | |
| ユーザー |
158b
|
| 提出日時 | 2017-05-28 23:15:41 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,488 bytes |
| コンパイル時間 | 554 ms |
| コンパイル使用メモリ | 74,192 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 15:57:54 |
| 合計ジャッジ時間 | 1,208 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 |
ソースコード
#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;
}
158b