結果

問題 No.517 壊れたアクセサリー
ユーザー bluemeganebluemegane
提出日時 2018-09-13 11:19:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,556 bytes
コンパイル時間 4,371 ms
コンパイル使用メモリ 104,536 KB
実行使用メモリ 24,484 KB
最終ジャッジ日時 2023-09-13 19:48:16
合計ジャッジ時間 5,194 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
20,524 KB
testcase_01 AC 62 ms
22,272 KB
testcase_02 AC 62 ms
20,316 KB
testcase_03 AC 65 ms
24,460 KB
testcase_04 AC 63 ms
22,368 KB
testcase_05 AC 63 ms
24,416 KB
testcase_06 AC 61 ms
22,352 KB
testcase_07 AC 61 ms
22,372 KB
testcase_08 AC 62 ms
22,456 KB
testcase_09 AC 64 ms
22,528 KB
testcase_10 AC 61 ms
22,440 KB
testcase_11 AC 64 ms
22,324 KB
testcase_12 AC 64 ms
22,360 KB
testcase_13 AC 65 ms
24,484 KB
testcase_14 AC 66 ms
22,476 KB
testcase_15 AC 65 ms
22,460 KB
testcase_16 AC 62 ms
24,380 KB
testcase_17 AC 61 ms
22,556 KB
testcase_18 AC 65 ms
24,456 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Linq;
using System.Collections.Generic;
using System;

public class Hello
{
    public static void Main()
    {
        var d = new Dictionary<char, List<char>>();
        var n = int.Parse(Console.ReadLine().Trim());
        setDic(d, n);
        n = int.Parse(Console.ReadLine().Trim());
        setDic(d, n);
        var res = getAns(d);
        Console.WriteLine(res);
    }
    public static string getAns (Dictionary<char,List<char>> d)
    {
        var c = d.Count(x => x.Value.Count() == 0);
        if (c >= 2) return "-1";
        var hs = new HashSet<char>();
        foreach (var x in d) hs.Add(x.Key);
        foreach (var x in d)
            foreach (var y in x.Value)
                hs.Remove(y);
        var nextchar = hs.First();
        var res = nextchar.ToString();
        while (true)
        {
            if (d[nextchar].Count() == 0) break;
            res += d[nextchar][0];
            nextchar = d[nextchar][0];
        }
        return res;
    }
    public static void setDic (Dictionary<char,List<char>> d,int n)
    {
        for (int i = 0; i < n; i++)
        {
            var s = Console.ReadLine().Trim();
            var sL = s.Length;
            for (int j = 0; j < s.Length-1; j++)
                if (d.ContainsKey(s[j]))
                {
                    if (d[s[j]].Count() == 0) d[s[j]].Add(s[j + 1]);
                }
                else d[s[j]] = new List<char>() { s[j + 1] };
            if (!d.ContainsKey(s[sL - 1]))
                d[s[sL - 1]] = new List<char>();
        }
    }
}
0