結果

問題 No.225 文字列変更(medium)
ユーザー lvs7klvs7k
提出日時 2018-11-15 22:18:48
言語 Haskell
(9.8.2)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,400 bytes
コンパイル時間 10,414 ms
コンパイル使用メモリ 188,344 KB
実行使用メモリ 18,928 KB
最終ジャッジ日時 2023-08-26 01:23:16
合計ジャッジ時間 12,531 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
13,736 KB
testcase_01 AC 32 ms
15,508 KB
testcase_02 AC 3 ms
7,372 KB
testcase_03 AC 3 ms
7,316 KB
testcase_04 AC 2 ms
7,356 KB
testcase_05 AC 2 ms
7,312 KB
testcase_06 AC 3 ms
7,444 KB
testcase_07 AC 2 ms
7,360 KB
testcase_08 AC 2 ms
7,372 KB
testcase_09 AC 3 ms
7,360 KB
testcase_10 AC 3 ms
7,368 KB
testcase_11 AC 3 ms
7,364 KB
testcase_12 AC 42 ms
17,804 KB
testcase_13 AC 42 ms
18,928 KB
testcase_14 AC 42 ms
18,888 KB
testcase_15 AC 42 ms
17,588 KB
testcase_16 AC 42 ms
17,308 KB
testcase_17 AC 42 ms
17,624 KB
testcase_18 AC 42 ms
17,104 KB
testcase_19 AC 42 ms
17,484 KB
testcase_20 AC 42 ms
17,544 KB
testcase_21 AC 42 ms
17,588 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns, FlexibleContexts #-}

import Control.Applicative
import Control.Monad
import Data.List
import Debug.Trace (traceShow, traceShowM)
import qualified Data.ByteString.Char8 as B
import Data.Maybe

import Control.Monad.ST
import Data.Array.ST

inf :: Int
inf = 10 ^ 9 + 7

solve n m ss ts = runST $ do
    dp <- newArray ((0, 0), (n, m)) inf :: ST s (STUArray s (Int, Int) Int)
    writeArray dp (0, 0) 0
    forM_ [0 .. n] $ \i -> do
        forM_ [0 .. m] $ \j -> do
            v1 <- if all (>= 1) [i, j] then Just <$> readArray dp (i - 1, j - 1) else return Nothing
            v2 <- if i >= 1 then Just <$> readArray dp (i - 1, j) else return Nothing
            v3 <- if j >= 1 then Just <$> readArray dp (i, j - 1) else return Nothing
            let vs = catMaybes [v1, v2, v3]
            when (not $ null vs) $ do
                if isJust v1
                    then do
                        if (ss `B.index` (i - 1)) == (ts `B.index` (j - 1))
                            then writeArray dp (i, j) (minimum $ [fromJust v1] ++ fmap (+ 1) (catMaybes [v2, v3]))
                            else writeArray dp (i, j) (minimum vs + 1)
                    else writeArray dp (i, j) (minimum vs + 1)
    readArray dp (n, m)

main :: IO ()
main = do
    [n, m] <- fmap read . words <$> getLine :: IO [Int]
    ss <- B.getLine
    ts <- B.getLine
    print $ solve n m ss ts
0