aydarbiktimirov

Combinations

Sep 28th, 2011
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List(sort, sortBy)
  2.  
  3. -- генерирует все сочетания
  4. findSolution l found len current
  5.     | found == len = [current]
  6.     | length l < len - found = []
  7.     | otherwise = (findSolution (tail l) (succ found) len (current ++ [head l])) ++ (findSolution (tail l) found len current)
  8.  
  9. -- обертка над верхней функцией
  10. solve l = concat [findSolution (sort l) 0 len [] | len <- [1..(length l)]]
  11.  
  12. -- замена в списке всех элементов a на b
  13. replace [] _ _ = []
  14. replace (x:xs) a b
  15.     | x == a = (b:(replace xs a b))
  16.     | otherwise = (x:(replace xs a b))
  17.  
  18. -- лексикографический порядок
  19. lexOrder :: [Int] -> [Int] -> Ordering
  20. lexOrder [] [] = EQ
  21. lexOrder [] (x:xs) = LT
  22. lexOrder (x:xs) [] = GT
  23. lexOrder (x:xs) (y:ys)
  24.     | x == y = lexOrder xs ys
  25.     | x < y = LT
  26.     | otherwise = GT
  27.  
  28. -- лексикографическая сортировка
  29. lexSort = sortBy lexOrder
  30.  
  31. main = do
  32.     input <- getLine -- считывание
  33.     print $ lexSort $ solve $ (read ("[" ++ (replace input ' ' ',') ++ "]") :: [Int]) -- вывод ответа
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment