結果
問題 | No.422 文字列変更 (Hard) |
ユーザー | eitaho |
提出日時 | 2016-09-10 00:20:09 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 814 ms / 3,000 ms |
コード長 | 5,617 bytes |
コンパイル時間 | 1,131 ms |
コンパイル使用メモリ | 111,488 KB |
実行使用メモリ | 88,448 KB |
最終ジャッジ日時 | 2024-11-17 06:44:36 |
合計ジャッジ時間 | 9,863 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 24 ms
18,304 KB |
testcase_01 | AC | 504 ms
67,328 KB |
testcase_02 | AC | 554 ms
72,448 KB |
testcase_03 | AC | 465 ms
63,360 KB |
testcase_04 | AC | 517 ms
67,328 KB |
testcase_05 | AC | 586 ms
74,880 KB |
testcase_06 | AC | 814 ms
88,448 KB |
testcase_07 | AC | 26 ms
19,200 KB |
testcase_08 | AC | 30 ms
20,900 KB |
testcase_09 | AC | 680 ms
82,816 KB |
testcase_10 | AC | 779 ms
86,656 KB |
testcase_11 | AC | 518 ms
68,352 KB |
testcase_12 | AC | 466 ms
62,720 KB |
testcase_13 | AC | 485 ms
64,640 KB |
testcase_14 | AC | 482 ms
65,792 KB |
testcase_15 | AC | 490 ms
66,304 KB |
testcase_16 | AC | 521 ms
68,096 KB |
コンパイルメッセージ
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) { // replace if (ti < M) { if (si < N) Update(si + 1, ti + 1, 0, currCost + (S[si] == T[ti] ? 0 : Replace)); } // delete Update(si + 1, ti, 2, currCost + FirstAddDelete); if (type == 2) Update(si + 1, ti, 2, currCost + AddDelete); } if (ti < M) { // add 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]; int ntype = prevType[Si, Ti, AnsType]; switch (AnsType) { case 0: ansS.Insert(0, S[nsi]); ansT.Insert(0, T[nti]); break; case 1: ansS.Insert(0, "-"); ansT.Insert(0, T[nti]); break; case 2: ansS.Insert(0, S[nsi]); ansT.Insert(0, "-"); break; } Si = nsi; Ti = nti; AnsType = ntype; } 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; } }