Codeforces 208A - A. Dubstep
- WUBを空白に置き換える
- 連続する空白をひとつの空白にまとめる
- 両端の空白を取り除く
-- "WUB"を取り除く(代わりに空白をおく) removeWUB :: String -> String removeWUB "" = "" removeWUB ('W':'U':'B':xs) = ' ' : removeWUB xs removeWUB (c:xs) = c : removeWUB xs -- 連続する空白を一つにまとめる packContinuousSpaces :: String -> String packContinuousSpaces "" = "" packContinuousSpaces xxs@(' ':' ':xs) = packContinuousSpaces $ tail xxs packContinuousSpaces (x:xs) = x : packContinuousSpaces xs trim :: String -> String trim s = reverse $ trim' $ reverse $ trim' s where trim' (' ':xs) = trim' xs trim' s = s calc = trim . packContinuousSpaces . removeWUB main = do s <- getLine putStrLn $ calc s
trimするときにreverseが2回必要なのは仕方がないのかな…。