結果

問題 No.422 文字列変更 (Hard)
ユーザー eitahoeitaho
提出日時 2016-09-10 00:20:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 774 ms / 3,000 ms
コード長 5,617 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 112,128 KB
実行使用メモリ 88,832 KB
最終ジャッジ日時 2024-04-28 14:14:12
合計ジャッジ時間 10,521 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
18,432 KB
testcase_01 AC 495 ms
67,328 KB
testcase_02 AC 539 ms
72,448 KB
testcase_03 AC 476 ms
63,360 KB
testcase_04 AC 483 ms
67,328 KB
testcase_05 AC 570 ms
74,752 KB
testcase_06 AC 774 ms
88,832 KB
testcase_07 AC 25 ms
19,200 KB
testcase_08 AC 27 ms
21,160 KB
testcase_09 AC 651 ms
82,944 KB
testcase_10 AC 724 ms
86,912 KB
testcase_11 AC 502 ms
68,352 KB
testcase_12 AC 430 ms
62,976 KB
testcase_13 AC 457 ms
64,640 KB
testcase_14 AC 462 ms
65,664 KB
testcase_15 AC 472 ms
66,176 KB
testcase_16 AC 481 ms
67,968 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.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;
    }
}
0