結果

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

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	int N; cin >> N;
	vector<string>A(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	int M; cin >> M;
	vector<string>B(M);
	for (int i = 0; i < M; i++) {
		cin >> B[i];
	}

	vector<bool>used(26);
	vector<vector<char>>link(26);
	for (int i = 0; i < 26; i++)link[i].resize(2);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < A[i].size(); j++) {
			used[A[i][j] - 'A'] = true;
			if (j > 0)link[A[i][j] - 'A'][0] = A[i][j-1];
			if(j < A[i].size()-1)link[A[i][j] - 'A'][1] = A[i][j + 1];
		}
	}
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < B[i].size(); j++) {
			used[B[i][j] - 'A'] = true;
			if (j > 0)link[B[i][j] - 'A'][0] = B[i][j - 1];
			if (j < B[i].size() - 1)link[B[i][j] - 'A'][1] = B[i][j + 1];
		}
	}

	int alp = 0;
	vector<int>cnt(3);
	for (int i = 0; i < 26; i++) {
		if (used[i]) {
			alp++;
			if (link[i][0] && link[i][1])cnt[2]++;
			else if (link[i][0] == 0 && link[i][1] == 0)cnt[0]++;
			else cnt[1]++;
		}
	}

	if (alp > 1){
		if (cnt[2] == alp - 2 && cnt[1] == 2 && cnt[0] == 0) {

			for (int j = 0; j < 26; j++) {
				if (used[j]) {
					int pos = link[j][0]-'A';
					string s = "";
					s.push_back(char('A' + j));
					while (pos>=0) {
						s.insert(s.begin(), pos + 'A');
						pos = link[pos][0]-'A';
					}
					pos = link[j][1]-'A';
					while (pos>=0) {
						s.push_back(pos + 'A');
						pos = link[pos][1]-'A';
					}

					cout << s << endl;
					break;
				}
			}
		}
		else cout << -1 << endl;
	}
	else {
		cout << A[0] << endl;
	}

    return 0;
}

0