import algorithm, sugar, macros, math, sequtils, sets, strutils, tables macro unpack(rhs: seq, cnt: static[int]): auto = let t = genSym(); result = quote do:(let `t` = `rhs`;()) if NimMinor <= 17: for i in 0..>=`(n: var int, m: int) = n = n shr m proc `?=`(n: var int, m: int) = n = max(n, m) proc newSeq2[T](n1, n2: Natural): seq[seq[T]] = newSeqWith(n1, newSeq[T](n2)) proc newSeq3[T](n1, n2, n3: Natural): seq[seq[seq[T]]] = newSeqWith(n1, newSeqWith(n2, newSeq[T](n3))) # -------------------------------------------------- # const base = 1550606 const modulus = 1000000007 type RollingHash = object hashed: seq[int] power: seq[int] proc initRollingHash(S: string): RollingHash = let N = S.len() - 1 result.hashed = newSeq[int](N + 1) result.power = newSeq[int](N + 1) result.power[0] = 1 for i in countup(1, N): result.hashed[i] = (result.hashed[i - 1] * base + ord(S[i])) mod modulus result.power[i] = result.power[i - 1] * base mod modulus proc toHash(self: RollingHash, l: Natural, r: Natural): int = result = (self.hashed[r] - self.hashed[l - 1] * self.power[r - l + 1]) mod modulus if result < 0: result += modulus # -------------------------------------------------- # var S = "#" & input(string) var M = input(int) var shash = initRollingHash(S) var k = S.len() - 1 var res = 0 for query in 1 .. M: var C = "#" & input(string) var chash = initRollingHash(C) var l = C.len() - 1 if k < l: continue for i in 1 .. (k - l + 1): if shash.toHash(i, i + l - 1) == chash.toHash(1, l): inc res echo res