import Data.List sortSnd x y | snd x < snd y = LT | otherwise = GT rInt :: String -> Int rInt = read divcards (c:cs) = (c, n) where n | head cs == 'A' = 1 | otherwise = rInt cs isfstChar c x = c == fst x sTupleList :: [(Char, Int)] -> [String] sTupleList [] = [] sTupleList (t:ts) = (fc ++ sc) : sTupleList ts where fc = [fst t] sc | snd t == 1 = "A" | otherwise = show $ snd t main = do getLine cards <- sortBy sortSnd . map divcards . words <$> getLine putStrLn $ unwords $ sTupleList $ (filter (isfstChar 'D') cards) ++ (filter (isfstChar 'C') cards) ++ (filter (isfstChar 'H') cards) ++ (filter (isfstChar 'S') cards)