Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Control.Monad
- import Data.HashMap.Lazy(fromList,(!))
- morseTable :: [(Char, String)]
- morseTable = zip ['a'..'z'] $ words ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."
- wordToMorse :: String -> Maybe String
- wordToMorse xs = return . concat =<< mapM (`lookup` morseTable) xs
- slice :: (Int, Int) -> [a] -> [a]
- slice (start, end) = take (end - start) . drop start
- decode :: String -> String -> Integer
- decode dict s = trace n where
- dict' = map (maybe "x" id . wordToMorse) $ lines dict
- dp = flip map [0..n] $ \i -> [j |
- mw <- dict', let j = i - length mw, j >= 0 && mw == slice (j, i) s]
- n = length s
- dpCount = map trace [0..n]
- trace :: Int -> Integer
- trace 0 = 1
- trace i = sum [dpCount !! j | j <- dp !! i]
- main :: IO ()
- main = print =<< liftM2 decode (readFile "wordlist.txt") getLine
Advertisement
Add Comment
Please, Sign In to add comment