Done several tasks
[cses-programming-tasks.git] / app / cses1622.hs
diff --git a/app/cses1622.hs b/app/cses1622.hs
new file mode 100644 (file)
index 0000000..8925b05
--- /dev/null
@@ -0,0 +1,22 @@
+import Data.List (sort, inits, tails, nub)
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let base = sort line1
+  let perms = permutations base
+  print $ length perms
+  putStrLn $ unlines perms
+
+permutations :: String -> [String]
+
+permutations [] = []
+permutations (x:[]) = [[x]]
+permutations xs = concatMap (\(y, ys) -> map (y:) (permutations ys)) (selects xs)
+
+selects :: String -> [(Char, String)]
+selects [] = []
+selects xs = nub [(head t, h ++ (tail t))
+                 | (h, t) <- zip (inits xs) (tails xs)
+                 , not (null t)]
+