結果

問題 No.517 壊れたアクセサリー
ユーザー nksk38nksk38
提出日時 2017-06-22 18:33:53
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,501 bytes
コンパイル時間 959 ms
コンパイル使用メモリ 91,104 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-10-02 12:20:08
合計ジャッジ時間 1,617 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pr;

int N,M,s_size;
map<string, int> m;
vector<string> a, b,c;

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++) {
		string s;
		cin >> s;
		a.push_back(s);
		s_size += s.size();
		for (int j = 0; j < s.size() - 1; j++) {
			string str = "";
			str += s[j],str+= s[j+1];
			m[str] = 1;
		}
	}
	cin >> M;
	for (int i = 0; i < M; i++) {
		string s;
		cin >> s;
		b.push_back(s);
		for (int j = 0; j < s.size() - 1; j++) {
			string str = "";
			str += s[j],str+= s[j + 1];
			m[str] = 1;
		}
	}

	if (s_size == 1) {
		cout << a[0] << endl;
		return 0;
	}

	string s;
	for (int j = 0; j < N; j++) {
		s = a[j];
		int used[30];
		for (int k = 0; k < N; k++)
			used[k] = 0;
		used[j] = 1;

		for (int i = 0; i < N; i++) {
			string str = "";
			str += s[s.size() - 1];
			str += a[i][0];
			if (used[i] == 0 && m[str] != 0) {
				s += a[i];
				m[str] = 1;
				used[i] = 1;
				if (s.size() == s_size) {
					cout << s << endl;
					return 0;
				}
			}
			str = "";
			str += a[i][a[i].size() - 1];
			str += s[0];
			if (used[i] == 0 && m[str] != 0) {
				s = a[i] + s;
				m[str] = 1;
				used[i] = 1;
				if (s.size() == s_size) {
					cout << s << endl;
					return 0;
				}
			}
		}
	}
	 cout << -1 << endl;
	return	0;
}
0