結果

問題 No.470 Inverse S+T Problem
ユーザー 14番14番
提出日時 2016-12-23 07:08:38
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 3,581 bytes
コンパイル時間 2,289 ms
コンパイル使用メモリ 106,628 KB
実行使用メモリ 27,600 KB
最終ジャッジ日時 2023-08-24 01:24:55
合計ジャッジ時間 6,304 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,464 KB
testcase_01 AC 59 ms
21,460 KB
testcase_02 AC 63 ms
23,472 KB
testcase_03 AC 63 ms
21,396 KB
testcase_04 AC 57 ms
21,380 KB
testcase_05 AC 63 ms
21,292 KB
testcase_06 AC 71 ms
23,772 KB
testcase_07 AC 72 ms
21,716 KB
testcase_08 AC 72 ms
23,744 KB
testcase_09 AC 62 ms
23,468 KB
testcase_10 AC 64 ms
21,448 KB
testcase_11 AC 324 ms
27,600 KB
testcase_12 AC 63 ms
23,440 KB
testcase_13 AC 63 ms
21,432 KB
testcase_14 AC 66 ms
21,364 KB
testcase_15 AC 60 ms
21,416 KB
testcase_16 AC 64 ms
21,516 KB
testcase_17 AC 64 ms
21,460 KB
testcase_18 AC 64 ms
23,532 KB
testcase_19 AC 79 ms
25,624 KB
testcase_20 AC 63 ms
23,444 KB
testcase_21 AC 63 ms
21,440 KB
testcase_22 AC 63 ms
21,488 KB
testcase_23 AC 64 ms
23,404 KB
testcase_24 AC 64 ms
21,496 KB
testcase_25 AC 57 ms
21,488 KB
testcase_26 AC 62 ms
21,432 KB
testcase_27 AC 58 ms
21,720 KB
testcase_28 AC 57 ms
23,536 KB
testcase_29 AC 55 ms
21,488 KB
testcase_30 AC 56 ms
21,520 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        int inputCount = int.Parse(Reader.ReadLine());
        this.WordList = new string[inputCount];
        for(int i=0; i<inputCount; i++) {
            this.WordList[i] = Reader.ReadLine();
        }
        if(inputCount > ('z'-'a') + ('Z' - 'A') + 2) {
            Console.WriteLine("Impossible");
            return;
        }
        Dictionary<char, bool> keyDic = new Dictionary<char, bool>();
        this.WordList.ToList().ForEach(a=>{
            keyDic[a[0]] = true;
            keyDic[a[2]] = true;
        });
        if(keyDic.Keys.Count() < inputCount) {
            Console.WriteLine("Impossible");
            return;
        }
        List<string> ans = this.GetAns(0, new Dictionary<string, int>());
        if(ans == null) {
            Console.WriteLine("Impossible");
        } else {
            ans.ForEach(a=>Console.WriteLine(a));
        }
    }

    private List<string> GetAns(int idx, Dictionary<string, int> dic) {
        string str1 = WordList[idx].Substring(0,1);
        string str2 = WordList[idx].Substring(1,2);
        if((!dic.ContainsKey(str1)) && (!dic.ContainsKey(str2))) {
            if(idx < this.WordList.Length - 1) {
                dic.Add(str1, idx);
                dic.Add(str2, idx);
                List<string> tmp = GetAns(idx+1, dic);
                dic.Remove(str1);
                dic.Remove(str2);
                if(tmp != null) {
                    tmp.Insert(0, str1 + " " + str2);
                    return tmp;
                }
            } else {
                return new string[] {str1 + " " + str2}.ToList();
            }
        }
        if(this.WordList[idx].Count(a=>a==this.WordList[idx][0]) == 3) {
            return null;
        }
        if(str1.Equals(this.WordList[idx].Substring(2))) {
            if(dic.ContainsKey(str1)) {
                return null;
            }
        }
        if(str2.Equals(this.WordList[idx].Substring(0,2))) {
            if(dic.ContainsKey(str2)) {
                return null;
            }
        }
        str1 = this.WordList[idx].Substring(0, 2);
        str2 = this.WordList[idx].Substring(2, 1);   
        if((!dic.ContainsKey(str1)) && (!dic.ContainsKey(str2))) {
            if(idx < this.WordList.Length - 1) {
                dic.Add(str1, idx);
                dic.Add(str2, idx);
                List<string> tmp = GetAns(idx+1, dic);
                dic.Remove(str1);
                dic.Remove(str2);
                if(tmp != null) {
                    tmp.Insert(0, str1 + " " + str2);
                    return tmp;
                }
            } else {
                return new string[] {str1 + " " + str2}.ToList();
            }
        }
        return null;
    }

    private string[] WordList;
    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"



";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0