結果

問題 No.225 文字列変更(medium)
ユーザー 14番14番
提出日時 2016-05-04 07:59:03
言語 C#(csc)
(csc 3.9.0)
結果
MLE  
実行時間 -
コード長 3,392 bytes
コンパイル時間 1,078 ms
コンパイル使用メモリ 105,984 KB
実行使用メモリ 589,580 KB
最終ジャッジ日時 2024-04-15 11:12:37
合計ジャッジ時間 18,921 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Collections.Generic;
using System.Text;
using System.Linq;
 
class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inputLength = Reader.GetInt();
        this.Input1 = Reader.ReadLine();
        this.Input2 = Reader.ReadLine();
        int ans = this.GetAns(this.Input1, this.Input2);
        
        Console.WriteLine(ans);

    }
    
    private Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();
    private int GetAns(string target, string goal) {
        if(!dic.ContainsKey(target)) {
            dic.Add(target, new Dictionary<string, int>());
        }
        if(dic[target].ContainsKey(goal)) {
            return dic[target][goal];
        }
        
        int ans = int.MaxValue;
        if(target.Length == 0 && goal.Length > 0) {
            ans = goal.Length;
        } else if(target.Length > 0 && goal.Length == 0) {
            ans = target.Length;
        } else if(target.Length == 0 && goal.Length == 0) {
            ans = 0;
        } else
        {
            if(target[0] == goal[0]) {
                int idx = 1;
                for(int i=1; i<Math.Min(target.Length, goal.Length); i++) {
                    if(target[i] != goal[i]) {
                        break;
                    }
                    idx = i + 1;
                }
                ans = this.GetAns(target.Substring(idx), goal.Substring(idx));
            }
            if(goal.Length >= 2 && goal.IndexOf(target[0]) > 0) 
            {
                int idx = goal.IndexOf(target[0]);
                ans = Math.Min(ans, this.GetAns(target, goal.Substring(idx)) + idx);
            }
            if(target.Length >= 2 && target.IndexOf(goal[0]) > 0) {
                int idx = target.IndexOf(goal[0]);
                ans = Math.Min(ans, this.GetAns(target.Substring(idx), goal) + idx);
            }
            if(target[0] != goal[0]) {
                ans = Math.Min(ans, this.GetAns(target.Substring(1), goal.Substring(1)) + 1);
            }
        }
        this.dic[target].Add(goal, ans);
        return ans;
    }
    
    private string Input1;
    private string Input2;
    

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


14 16
pirikapirirara
poporinapeperuto


 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0