Guest User

Untitled

a guest
Aug 26th, 2014
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. import Data.HashMap.Lazy(fromList,(!))
  3.  
  4. morseTable :: [(Char, String)]
  5. morseTable = zip ['a'..'z'] $ words ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."
  6.  
  7. wordToMorse :: String -> Maybe String
  8. wordToMorse xs = return . concat =<< mapM (`lookup` morseTable) xs
  9.  
  10. slice :: (Int, Int) -> [a] -> [a]
  11. slice (start, end) = take (end - start) . drop start
  12.  
  13. decode :: String -> String -> Integer
  14. decode dict s = trace n where
  15.   dict' = map (maybe "x" id . wordToMorse) $ lines dict
  16.  dp = flip map [0..n] $ \i -> [j |
  17.        mw <- dict', let j = i - length mw, j >= 0 && mw == slice (j, i) s]
  18.   n = length s
  19.   dpCount = map trace [0..n]
  20.   trace :: Int -> Integer
  21.   trace 0 = 1
  22.   trace i = sum [dpCount !! j | j <- dp !! i]
  23.  
  24. main :: IO ()
  25. main = print =<< liftM2 decode (readFile "wordlist.txt") getLine
Advertisement
Add Comment
Please, Sign In to add comment