haskell - Why does this code run slower on spoj, triggering the timeout? -
i trying solve problem on spoj: http://www.spoj.com/problems/palin/
my code works fine , fast on laptop (even slower cpu 1 spoj provides) spoj keeps giving me time exceeded:
import control.monad main = num <- getline inputs <- replicatem (read num) getline test inputs test [] = return () test (l:ls) = putstrln (send l) test ls send :: string -> string send str | odd (length str) = makepalindrome str (take (length str `div` 2 + 1) str ++ ( reverse $ take (length str `div` 2) str)) | otherwise = makepalindrome str (take (length str `div` 2) str ++ (reverse $ take (length str `div` 2) str)) makepalindrome :: string -> string -> string makepalindrome str pal | (read str :: integer) < (read pal :: integer) = pal | otherwise = makepalindrome str (nextpalindrome pal) nextpalindrome :: string -> string nextpalindrome (x:xs) = succ' ++ (reverse $ take rightlen succ') strlen = length (x:xs) allnine = ( (==) '9') odd' = odd strlen left = if odd' take (strlen `div` 2 + 1) (x:xs) else take (strlen `div` 2) (x:xs) rightlen = strlen - (length left) succ' = show (read left + 1) checkpalindrome :: string -> bool checkpalindrome str = str == (reverse str)
Comments
Post a Comment