import Control.Monad import Data.List import Data.Char import Data.IntMap ((!)) import qualified Data.IntMap as M main = do s <- getLine getLine cs <- lines <$> getContents print (searchString s cs) searchString s = sum . map (match s) match s c = length (filter (== (hash c)) (t!(length c))) where t = M.fromDistinctAscList [(k,v) | k <- [1..5000], let v = hashp k s] hash c = sum (zipWith (*) (map (\e -> ord e - 65) c) (iterate (*26) 1)) hashp l s = unfoldr (\(s',l') -> guard (l' >= l) >> Just (hash (take l s'), (tail s', pred l'))) (s, length s)