結果

問題 No.422 文字列変更 (Hard)
ユーザー eitahoeitaho
提出日時 2016-09-10 00:10:51
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 5,617 bytes
コンパイル時間 2,582 ms
コンパイル使用メモリ 112,384 KB
実行使用メモリ 88,576 KB
最終ジャッジ日時 2024-11-17 06:43:03
合計ジャッジ時間 11,681 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 28 ms
18,944 KB
testcase_08 WA -
testcase_09 AC 783 ms
82,816 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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, T[nti]);
                    ansT.Insert(0, T[nti]);
                    break;
                case 1:
                    ansS.Insert(0, "-");
                    ansT.Insert(0, T[nti]);
                    break;
                case 2:
                    ansS.Insert(0, "-");
                    ansT.Insert(0, S[nsi]);
                    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