[Haskell-beginners] Recursion/dynamic programming optimization

ivovnenko ivovnenko at gmail.com
Fri Mar 19 16:41:48 EDT 2010


Hi.
I tried to solve one of the google code jam's problems using haskel
http://code.google.com/codejam/contest/dashboard?c=90101#s=p2:

w :: Eq a => [a] -> [a] -> Int
w text str = w' (reverse text) (reverse str)
w' :: Eq a => [a] -> [a] -> Int
-- Init
w' _ [] = 0
w' [] _ = 0
-- For one symbol string
w' text [a] =
        if head text == a then
                w' (tail text) [a] +1
        else
                w' (tail text) [a]
-- General case
w' text str =
        if head str == head text then
                w' (tail text) (tail str) + w' (tail text) str
        else
                w' (tail text) str

and it worked great on small inputs, not on large:

w "viwiwhiuwwwgxzseebneesgzxiekzeeyivrbbhzrlllccxczyurxfhhccghkzvxusfbcucyvncpzpvfvccxcvbqnqogobzoiorxrbikyzzbobpmmmmpmvrzyivpfmmuzxixhqpeebyekkxffeyexeiukuebgbrukzefeu
rkxy z h x vkq fsyp iy  tfyzbptygtsrkhbytvruztzbttnoxuquokipyyqoik ib
xu    gxchcnccuccyfcccccsczccskifcprgovryxuxoozqoozsvudyfkdpkkxyhdvfsrnvpnpzvrhqhbduidkdpdysinyxnuxnxipddevqhgeufxeepyyezbekyviieeeryiexynevbzfeezryizvepynefffy
y v zuzrbpzn  sz y
vzirkxjniuqvjjbjgubnjkujhjpiyanaabrqagzqqvspqpkvqyuhrgupbaiaasxybiygmmfimyvykkmrzum"
"welcome to code jam"

Can you please advice how to optimize this solution? Thanks.

Regards, Ivan Vovnenko


More information about the Beginners mailing list