結果

問題 No.225 文字列変更(medium)
ユーザー htensaihtensai
提出日時 2020-01-15 19:34:48
言語 Java19
(openjdk 21)
結果
AC  
実行時間 3,229 ms / 5,000 ms
コード長 1,272 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 74,732 KB
実行使用メモリ 271,312 KB
最終ジャッジ日時 2023-09-02 10:28:46
合計ジャッジ時間 37,532 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,291 ms
123,588 KB
testcase_01 AC 2,269 ms
146,596 KB
testcase_02 AC 123 ms
56,428 KB
testcase_03 AC 122 ms
55,544 KB
testcase_04 AC 124 ms
56,056 KB
testcase_05 AC 124 ms
55,920 KB
testcase_06 AC 132 ms
55,740 KB
testcase_07 AC 126 ms
56,400 KB
testcase_08 AC 126 ms
55,804 KB
testcase_09 AC 132 ms
55,716 KB
testcase_10 AC 135 ms
55,764 KB
testcase_11 AC 131 ms
55,920 KB
testcase_12 AC 2,797 ms
248,964 KB
testcase_13 AC 3,229 ms
231,656 KB
testcase_14 AC 3,168 ms
238,060 KB
testcase_15 AC 1,892 ms
229,296 KB
testcase_16 AC 3,134 ms
230,608 KB
testcase_17 AC 1,934 ms
265,300 KB
testcase_18 AC 2,493 ms
271,312 KB
testcase_19 AC 3,101 ms
222,840 KB
testcase_20 AC 2,696 ms
250,308 KB
testcase_21 AC 3,070 ms
235,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;

public class Main {
    static HashMap<String, HashMap<String, Integer>> all = new HashMap<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        System.out.println(getCount(sc.next(), sc.next()));
    }
    
    static int getCount(String aSb, String bSb) {
        HashMap<String, Integer> tmp = all.get(aSb);
        if (tmp != null && tmp.containsKey(bSb)) {
            return tmp.get(bSb);
        }
        int min = Integer.MAX_VALUE;
        if (aSb.length() == 0 || bSb.length() == 0) {
            return aSb.length() + bSb.length();
        }
        String aaSb = aSb.substring(1, aSb.length());
        String bbSb = bSb.substring(1, bSb.length());
        if (aSb.charAt(0) == bSb.charAt(0)) {
            min = getCount(aaSb, bbSb);
        } else {
            min = Math.min(min, getCount(aSb, bbSb) + 1);
            min = Math.min(min, getCount(aaSb, bSb) + 1);
            min = Math.min(min, getCount(aaSb, bbSb) + 1);
        }
        if (tmp == null) {
            tmp = new HashMap<String, Integer>();
            all.put(aSb, tmp);
        }
        tmp.put(bSb, min);
        return min;
    }
}
0