最大部分列和問題
「与えられたリストの連続する部分列の内、和が最大となるものを求め、その和を返せ」というプログラムを書け
http://d.hatena.ne.jp/yokoyamatetsuo/20050224/p2
solve = maximum . candidates . preproc preproc = map sum . groupBy (((>= 0).) . (*)) . dropWhile (<= 0) -- [+,-,+,...] という入力を仮定 candidates (x:y:z:cs) = x:candidates (s:cs) where s = if x + y + z > z then x + y + z else z candidates (x:_) = [x] candidates [] = [0]
Re:http://www.lab2.kuis.kyoto-u.ac.jp/~hanatani/tdiary/?date=20050223#p05
5行目が違うと思います。g [-1,1]とか。
g xs = f xs ([], 0) ([], 0) f (x:xs) (ys, sy) (zs, sz) | x < 0 = if sy > sz then f xs (x:ys, x+sy) (ys, sy) else f xs (x:ys, x+sy) (zs, sz) | True = if sy < 0 then f xs ([x], x) (zs, sz) -- Corrected else f xs (x:ys, x+sy) (zs, sz) f [] (ys, sy) (zs, sz) = if sy > sz then reverse ys else reverse zs