“TŒ^–â‘è    ŽŸ‚Ì“TŒ^–â‘è‚Ö    ‘O‚Ì“TŒ^–â‘è‚Ö

“TŒ^–â‘è(ã‹‰) B •ÒW‹——£


–â‘è‚ւ̃Šƒ“ƒN


C#‚̃\[ƒX

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 6");
            WillReturn.Add("abcde");
            WillReturn.Add("bcdefg");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        string S1 = InputList[1];
        int UB_X = S1.Length;

        string S2 = InputList[2];
        int UB_Y = S2.Length;

        int[,] DPArr = new int[UB_X + 1, UB_Y + 1];

        // ‰Šú’l‚̐ݒè
        for (int X = 0; X <= UB_X; X++) {
            DPArr[X, 0] = X;
        }
        for (int Y = 0; Y <= UB_Y; Y++) {
            DPArr[0, Y] = Y;
        }

        // –á‚€DP‚ʼnð‚­
        for (int X = 1; X <= UB_X; X++) {
            for (int Y = 1; Y <= UB_Y; Y++) {
                char CurrStr1 = S1[X - 1];
                char CurrStr2 = S2[Y - 1];

                var KouhoList = new List<int>();

                // Œó•â1 ã‚̃}ƒX‚Ì’l+1
                KouhoList.Add(DPArr[X, Y - 1] + 1);

                // Œó•â2 ¶‚̃}ƒX‚Ì’l+1
                KouhoList.Add(DPArr[X - 1, Y] + 1);

                // Œó•â3‚Ì1 ¶ã‚̃}ƒX‚Ì’l+1
                if (CurrStr1 != CurrStr2) {
                    KouhoList.Add(DPArr[X - 1, Y - 1] + 1);
                }
                else { // Œó•â3‚Ì2 ¶ã‚̃}ƒX‚Ì’l
                    KouhoList.Add(DPArr[X - 1, Y - 1]);
                }

                DPArr[X, Y] = KouhoList.Min();
            }
        }
        Console.WriteLine(DPArr[UB_X, UB_Y]);
    }
}


‰ðà

–á‚€DP‚Å‰ð‚¢‚Ä‚Ü‚·B


—Þ‘è

DPL_1_E: Edit Distance (Levenshtein Distance)