2012-01-08 计算所有后缀的排名 占位。以后补上解释。 Prefix-doubling algorithm。 1234567891011121314151617181920import Control.Applicativeimport Control.Arrowimport Control.Monadimport Control.Monad.Instancesimport Data.Arrayimport Data.Functionimport Data.Listrank :: (Ord a) => [a] -> [Int]rank = elems . liftM2 array ((,) 0 . pred . length) (map (first snd) . concat . label . groupBy ((==) `on` fst) . sort . flip zip [0..]) where label = zipWith (flip (map . flip (,))) <*> scanl (+) 0 . map lengthrankTails1 :: (Ord a) => [a] -> [Int]rankTails1 = liftM3 applyUntil (((and . elems).) . (.flip zip (repeat True)) . accumArray (||) False . (,) 0 . pred . length) (const $ map reorder (iterate (*2) 1)) rank where reorder = (rank.) . (zip <*>) . ((++ repeat (-1)).) . drop applyUntil p fs x = head . dropWhile (not . p) $ scanl (flip ($)) x fsmain = getLine >>= print . rankTails1 Newer Tiling window manager的应用与设计 Older 网络学堂feeds2mail