結果

問題 No.517 壊れたアクセサリー
ユーザー hiyokko2hiyokko2
提出日時 2017-05-28 21:54:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,307 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 91,520 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:09:59
合計ジャッジ時間 1,614 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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