結果

問題 No.517 壊れたアクセサリー
ユーザー hiyokko2hiyokko2
提出日時 2017-05-28 21:54:47
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,307 bytes
コンパイル時間 922 ms
コンパイル使用メモリ 89,856 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-21 15:24:24
合計ジャッジ時間 1,505 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cstring>

#define int long long
//typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;

int N, M;
string in[52];
int len;
string ans;
map<string, bool> checked;

void dfs(string str)
{
    if (checked[str]) {
        return;
    } else {
        checked[str] = true;
    }
    
    if (str.length() > ans.length()) {
        ans = str;
        //cout << "ans = " << ans << endl;
    }

    char fst = str[0];
    char lst = str[str.length()-1];

    rep(i, N + M) {
        if (in[i].find(fst) != string::npos && in[i].find(fst) >= 1) {
            dfs(in[i].substr(0, in[i].find(fst)) + str);
        }
        if (in[i].find(lst) != string::npos && in[i].find(lst) < in[i].length() - 1) {
            dfs(str + in[i].substr(in[i].find(lst) + 1));
        }
    }
}

signed main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    cin >> N;
    rep(i,N) {
        cin >> in[i];
        len += in[i].length();
    }
    cin >> M;
    rep(i,M) cin >> in[N+i];

    dfs(in[0]);

    if (ans.length() == len) {
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
}
0