結果

問題 No.517 壊れたアクセサリー
ユーザー hiyokko2hiyokko2
提出日時 2017-05-28 21:53:11
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,187 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 83,488 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-21 14:09:40
合計ジャッジ時間 4,503 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;

void dfs(string str)
{
    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