結果

問題 No.517 壊れたアクセサリー
ユーザー nukachanukacha
提出日時 2017-05-30 06:54:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 724 ms
コンパイル使用メモリ 74,368 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 17:58:51
合計ジャッジ時間 1,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

int N, M;
std::vector<std::string> A, B;

int main()
{
    int chain;
    std::cin >> N;
    A.resize(N);
    for (int i = 0; i < N; i++)
    {
        std::cin >> A[i];
    }
    std::cin >> M;
    B.resize(M);
    for (int i = 0; i < M; i++)
    {
        std::cin >> B[i];
    }
    if (N == 1)
    {
        std::cout << A[0] << std::endl;
        return 0;
    }
    else if (M == 1)
    {
        std::cout << B[0] << std::endl;
        return 0;
    }
    chain = N - 1;
    while (chain > 0)
    {
        int old_chain = chain;
        for (int i = 0; i < A.size(); i++)
        {
            for (int j = i + 1; j < A.size(); j++)
            {
                if (A[i] == "")
                {
                    continue;
                }
                std::string tmp1, tmp2;
                tmp1 = std::string() + A[i].back() + A[j].front();
                tmp2 = std::string() + A[j].back() + A[i].front();
                for (int k = 0; k < M; k++)
                {
                    if (B[k].find(tmp1) != std::string::npos)
                    {
                        A[i] += A[j];
                        A[j] = "";
                        chain--;
                        break;
                    }
                    if (B[k].find(tmp2) != std::string::npos)
                    {
                        A[j] += A[i];
                        A[i] = "";
                        chain--;
                        break;
                    }
                }
            }
        }
        if (chain == old_chain)
        {
            std::cout << -1 << std::endl;
            return 0;
        }
    }
    for (int i = 0; i < N; i++) {
        if (A[i] != "") {
            std::cout << A[i] << std::endl;
            break;
        }
    }
}
0