結果
問題 | No.517 壊れたアクセサリー |
ユーザー | 14番 |
提出日時 | 2017-05-28 21:49:08 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 31 ms / 2,000 ms |
コード長 | 3,682 bytes |
コンパイル時間 | 956 ms |
コンパイル使用メモリ | 113,880 KB |
実行使用メモリ | 18,944 KB |
最終ジャッジ日時 | 2024-09-21 15:22:58 |
合計ジャッジ時間 | 2,253 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { List<string> ac1 = new List<string>(); int cnt = int.Parse(Reader.ReadLine()); for (int i = 0; i < cnt; i++) { ac1.Add(Reader.ReadLine()); } List<string> ac2 = new List<string>(); cnt = int.Parse(Reader.ReadLine()); for (int i = 0; i < cnt; i++) { ac2.Add(Reader.ReadLine()); } string ans = this.GetAns(ac1, ac2); Console.WriteLine(ans); } private string GetAns(List<string> ac1, List<string> ac2) { if (ac1.Count == 1) { return ac1[0]; } if (ac2.Count == 1) { return ac2[0]; } if(ac1.Count <= 0 || ac2.Count <= 0) { return "-1"; } for (int i = 0; i < ac1.Count; i++) { char target = ac1[i].First(); string tmp = ac2.Where(a => a.Contains(target)).First(); int idx = tmp.IndexOf(target); if (idx > 0) { char prevChar = tmp[idx - 1]; int idxPrev = ac1.IndexOf(ac1.Find(a => a.Last() == prevChar)); return GetAns(Join(ac1, idxPrev, i), ac2); } target = ac1[i].Last(); tmp = ac2.Where(a => a.Contains(target)).First(); idx = tmp.IndexOf(target); if (tmp.Last() != target) { char nextChar = tmp[idx + 1]; int idxNext = ac1.IndexOf(ac1.Find(a => a.First() == nextChar)); return GetAns(Join(ac1, i, idxNext), ac2); } } for (int i = 0; i < ac2.Count; i++) { char target = ac2[i].First(); string tmp = ac1.Where(a => a.Contains(target)).First(); int idx = tmp.IndexOf(target); if (idx > 0) { char prevChar = tmp[idx - 1]; int idxPrev = ac2.IndexOf(ac2.Find(a => a.Last() == prevChar)); return GetAns(ac1, Join(ac2, idxPrev, i)); } target = ac2[i].Last(); tmp = ac1.Where(a => a.Contains(target)).First(); idx = tmp.IndexOf(target); if (tmp.Last() != target) { char nextChar = tmp[idx + 1]; int idxNext = ac2.IndexOf(ac2.Find(a => a.First() == nextChar)); return GetAns(ac1, Join(ac2, i, idxNext)); } } return "-1"; } private List<string> Join(List<string> list, int idx1, int idx2) { List<string> ret = new List<string>(); ret.AddRange(list); ret.RemoveAt(Math.Max(idx1, idx2)); ret.RemoveAt(Math.Min(idx1, idx2)); ret.Add(list[idx1] + list[idx2]); return ret; } public class Reader { public static bool IsDebug = false; private static String PlainInput = @" 2 EA T 2 T EA "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }