import scala.math._ import scala.collection.mutable.{HashMap => MHashMap, _} import scala.collection.immutable._ object Main{ def main(args: Array[String]): Unit = { val sc = new java.util.Scanner(System.in) val pw = new java.io.PrintWriter(System.out) val s = sc.next val m = sc.nextInt val rh = new RollingHash(s) val maps: Array[MHashMap[Long, Int]] = Array.fill(10)(new MHashMap) for(l <- 1 to 10){ for(i <- 0 to (s.length - l)){ val j = i + l val a = rh.hash(i,j) maps(l-1).get(a) match{ case Some(x) => maps(l-1).update(a,x+1) case None => maps(l-1).update(a,1) } } } pw.println( (1 to m).map(_ => { val c = sc.next maps(c.length-1).get(rh.hash(c)) match{ case Some(x) => x case None => 0 } }).sum) pw.flush() } } class RollingHash(s: String){ private val base: Long = 29 private val m: Long = 1000000007 private val sl = s.length private val hash: Array[Long] = s.scanLeft(0: Long)((a,b) => (a*base+b)%m).toArray private val pow: Array[Long] = (1 to sl).scanLeft(1: Long)((a,b) => a*base%m).toArray def hash(i: Int, j: Int): Long = (hash(j) - hash(i) * pow(j-i) + m*m) % m def hash(p: String): Long = p.foldLeft(0: Long)((a,b) => (a*base+b)%m) def find(p: String): Array[Int] = { val pl = p.length val phash: Long = p.foldLeft(0: Long)((a,b) => (a*base+b)%m) (0 to (sl-pl)).filter(i => { val j = i + pl hash(i,j) == phash }).toArray } }