結果
問題 | No.422 文字列変更 (Hard) |
ユーザー | eitaho |
提出日時 | 2016-09-09 23:50:29 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,348 bytes |
コンパイル時間 | 1,120 ms |
コンパイル使用メモリ | 112,128 KB |
実行使用メモリ | 88,832 KB |
最終ジャッジ日時 | 2024-11-17 06:39:24 |
合計ジャッジ時間 | 11,003 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 763 ms
82,560 KB |
testcase_10 | WA | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Diagnostics; using Enu = System.Linq.Enumerable; public class Program { static readonly int INF = (int)1e9; static readonly int Replace = 5; static readonly int FirstAddDelete = 9; static readonly int AddDelete = 2; public void Solve() { int N = Reader.Int(), M = Reader.Int(); var S = Reader.String(); var T = Reader.String(); Func<int, int> Cost = len => 9 + (len - 1) * 2; var minCost = new int[N + 1, M + 1, 3]; var prevSi = new int[N + 1, M + 1, 3]; var prevTi = new int[N + 1, M + 1, 3]; var prevType = new int[N + 1, M + 1, 3]; Fill(minCost, INF); minCost[0, 0, 0] = 0; for (int si = 0; si <= N; si++) for (int ti = 0; ti <= M; ti++) for (int type = 0; type < 3 && (si < N || ti < M); type++) { Action<int, int, int, int> Update = (nsi, nti, ntype, cost) => { if (cost >= minCost[nsi, nti, ntype]) return; minCost[nsi, nti, ntype] = cost; prevSi[nsi, nti, ntype] = si; prevTi[nsi, nti, ntype] = ti; prevType[nsi, nti, ntype] = type; }; int currCost = minCost[si, ti, type]; if (si < N) { if (ti < M) { if (si < N) Update(si + 1, ti + 1, 0, currCost + (S[si] == T[ti] ? 0 : Replace)); } Update(si + 1, ti, 2, currCost + FirstAddDelete); if (type == 2) Update(si + 1, ti, 2, currCost + AddDelete); } if (ti < M) { Update(si, ti + 1, 1, currCost + FirstAddDelete); if (type == 1) Update(si, ti + 1, 1, currCost + AddDelete); } } int ans = int.MaxValue, Si = N, Ti = M, AnsType = 0; for (int type = 0; type < 3; type++) if (minCost[N, M, type] < ans) { ans = minCost[N, M, type]; AnsType = type; } var ansS = new StringBuilder(); var ansT = new StringBuilder(); while (Si > 0 || Ti > 0) { int nsi = prevSi[Si, Ti, AnsType]; int nti = prevTi[Si, Ti, AnsType]; switch (prevType[Si, Ti, AnsType]) { case 0: case 1: ansS.Insert(0, T[nti]); ansT.Insert(0, T[nti]); break; case 2: ansT.Insert(0, "-"); break; } AnsType = prevType[Si, Ti, AnsType]; Si = nsi; Ti = nti; } Console.WriteLine(ans); Console.WriteLine(ansS); Console.WriteLine(ansT); Console.ReadLine(); } void Fill<T>(T[, ,] X, T val) { int A = X.GetLength(0), B = X.GetLength(1), C = X.GetLength(2); for (int a = 0; a < A; a++) for (int b = 0; b < B; b++) for (int c = 0; c < C; c++) X[a, b, c] = val; } } class Entry { static void Main() { new Program().Solve(); } } class Reader { static TextReader reader = Console.In; static readonly char[] separator = { ' ' }; static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; static string[] A = new string[0]; static int i; static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Range(N, Int); } public static int[][] IntTable(int H) { return Range(H, IntLine); } public static string[] StringArray(int N) { return Range(N, Next); } public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); } public static string Line() { return reader.ReadLine().Trim(); } static string[] Split(string s) { return s.Split(separator, op); } static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; } static string Next() { CheckNext(); return A[i++]; } static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }