aydarbiktimirov icon

Combinations

aydarbiktimirov | PRO | 09/28/11 07:00:11 AM UTC | 0 ⭐ | 370 👁️ | Never ⏰ | []
Haskell |

1.11 KB

|

None

|

0 👍

/

0 👎

import Data.List(sort, sortBy)
 
-- генерирует все сочетания
findSolution l found len current
    | found == len = [current]
    | length l < len - found = []
    | otherwise = (findSolution (tail l) (succ found) len (current ++ [head l])) ++ (findSolution (tail l) found len current)
 
-- обертка над верхней функцией
solve l = concat [findSolution (sort l) 0 len [] | len <- [1..(length l)]]
 
-- замена в списке всех элементов a на b
replace [] _ _ = []
replace (x:xs) a b
    | x == a = (b:(replace xs a b))
    | otherwise = (x:(replace xs a b))
 
-- лексикографический порядок
lexOrder :: [Int] -> [Int] -> Ordering
lexOrder [] [] = EQ
lexOrder [] (x:xs) = LT
lexOrder (x:xs) [] = GT
lexOrder (x:xs) (y:ys)
    | x == y = lexOrder xs ys
    | x < y = LT
    | otherwise = GT
 
-- лексикографическая сортировка
lexSort = sortBy lexOrder
 
main = do
    input <- getLine -- считывание
    print $ lexSort $ solve $ (read ("[" ++ (replace input ' ' ',') ++ "]") :: [Int]) -- вывод ответа
 
 

Comments