Codeforces 219A - A. k-String

219A - A. k-String

  • 文字列をソートする。
  • 同じ文字でグルーピング。
  • グルーピングした文字列の長さが k の倍数なら、k-string は作れる。そうでなければ k-string は作れない
import Data.List

calc :: Int -> String -> Maybe String
calc k s = if all (\t -> length t `mod` k == 0) xs
           then Just $ (foldl1 (++) . replicate k) $ kString1 xs
           else Nothing
  where
    xs = group $ sort s
    kString1 []     = ""
    kString1 (x:xs) = take (length x `div` k) x ++ kString1 xs

main = do s <- getLine
          t <- getLine
          case calc (read s) t of
            Just x  -> putStrLn x
            Nothing -> print (-1) -- -1 は括弧で囲む必要がある