結果

問題 No.470 Inverse S+T Problem
ユーザー 14番
提出日時 2016-12-23 07:08:38
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 3,581 bytes
コンパイル時間 977 ms
コンパイル使用メモリ 108,800 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-12-22 13:38:04
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
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();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0