Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created August 24, 2012 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dradtke/3454978 to your computer and use it in GitHub Desktop.
Save dradtke/3454978 to your computer and use it in GitHub Desktop.
Haskell solution for CodeChef - Ambiguous Permutations
import Control.Monad
import Data.List
import Data.Maybe
import System.Exit
main = do
-- get the number for this case, and exit if it's equal to 0
n <- liftM read getLine :: IO (Int)
when (n == 0) exitSuccess
-- read the input and calculate the answer
rawInput <- liftM words getLine :: IO [String]
let p = map read rawInput :: [Int]
putStrLn $ solve p n
-- recurse
main
solve :: [Int] -> Int -> String
solve p n
| p == inverse p n = "ambiguous"
| otherwise = "not ambiguous"
-- convert a list of numbers into its inverse permutation
inverse :: [Int] -> Int -> [Int]
inverse p n = f [1..n]
where f [] = []
f (x:xs) = (fromJust $ findIndex (==x) p) + 1 : f xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment