久々のParsec
http://www.acm-japan.org/past-icpc/domestic2006/contest/all_ja.html の Problem E
最近ちっこい問題を適当に解いてるけど、これは気分転換にいいかもしれん。
import Control.Monad paren :: Parser String paren = do n <- many1 digit str <- between (char '(') (char ')') genome return (concat $ replicate (read n) str) word :: Parser String word = many1 letter genome :: Parser String genome = liftM concat $ many (paren <|> word) expand :: String -> String expand genomes = case parse genome "" genomes of Right a -> a _ -> "parse error"
Main> expand "2(ab3(d)c)"
"abdddcabdddc"
Main>