Posted at

第18回オフラインリアルタイムどう書くの参考問題をHaskellで解いた

More than 5 years have passed since last update.

第18回オフラインリアルタイムどう書くの参考問題をHaskellで解きました。

問題の詳細は山折り谷折り〜 横へな 2014.2.1 参考問題


yamatani.hs


invert [] = []
invert xs = map invert' $ reverse xs
where
invert' 'm' = 'V'
invert' 'V' = 'm'

j [] = "V"
j xs = xs ++ "V" ++ (invert xs)

l [] = "V"
l xs = (invert xs) ++ "V" ++ xs

s [] = "Vm"
s xs = xs ++ "V" ++ (invert xs) ++ "m" ++ xs

z [] = "mV"
z xs = xs ++ "m" ++ (invert xs) ++ "V" ++ xs

u [] = "VV"
u xs = (invert xs) ++ "V" ++ xs ++ "V" ++ (invert xs)

solve xs = foldr applyFunc "" xs
where
applyFunc :: Char -> String -> String
applyFunc c xs = charToFunc c xs

charToFunc :: Char -> (String -> String)
charToFunc 'J' = j
charToFunc 'L' = l
charToFunc 'S' = s
charToFunc 'Z' = z
charToFunc 'U' = u

main = do
line <- getLine
let [input, expect] = words line
let output = solve input
putStrLn $ if expect == output then "OK" else "NG"
main


他の過去問も解いています。(解けそうな問題から書いてます。。。汗)