Problem:
G： 検閲により置換 (Censored String)

Problem Statement

You are given the string S, and the set of strings \mathcal P whose size is N. Now we consider applying the following operation to S:

Choose exactly one character in S, and replace it with '*'.

Let s_i be a i-th character of S, S_{ij} be a consecutive substring in S such that S = s_i s_{i+1} $\cdots$ s_j, and p_k as the k-th string of P. Your task is to calculate the minimum number of operations for satisfying the following condition.

For all pairs (S_{ij}, p_k), it is satisfied that S_{ij} \neq p_k.

Input

An input is given in the following format.

S
N
p_1
p_2
$\vdots$
p_N

In line 1, you are given the string S.

In line 2, given an integer N. N means the size of P.

From line 3 to the end, given p_i, the i-th string of P.

Constraints

1 \leq |S| \leq 10^5

1 \leq N \leq 10^5

1 \leq |p_1| + |p_2| + $\cdots$ + |p_N| \leq 10^5

If i \neq j, it is guaranteed that p_i \neq p_j

S, and p_i include lowercase letters only

Output

Print the minimum number of operations in one line.

Sample Input 1

brainfuck
1
fuck

Sample Output 1

1

For example, if you change "brainfuck" into "brainf*ck", you can satisfy the condition. You have to perform operation at least once, and it is the minimum value.

Sample Input 2

aaa
1
a

Sample Output 2

3

You have to operate for all characters.

Sample Input 3

hellshakeyano
3
hell
shake
key

Sample Output 3

2

In this case, for example, if you change "hellshakeyano" into "h*llsha*eyano", you can satisfy the condition. Notice if you replace one character, it might happen that several strings in the set P, which appear as the consecutive substring of S, simultaneously disappear.

Solution in Scala (judge verdict: Accepted)

import Main.AhoCorasick.Automaton

import scala.annotation.tailrec
import scala.collection.mutable
object Main extends App {
  import scala.io.StdIn._

  val s = readLine.trim
  val n = readInt
  val words = Array.tabulate(n){_ ⇒ readLine.trim}
  println(solve(s, words))
  def solve(target: String, words: Array[String]): Int = {
    val automaton = AhoCorasick(words)
    @tailrec
    def sub(state: Automaton, lastRemoved: Int = 0, current: Int = 0, count: Int = 0): Int = {
      if (current == target.length) {
        if (state.matched.nonEmpty && (count == 0 || state.matched.exists(_.length + lastRemoved <= current))) count + 1
        else count
      }
      else if (state.matched.nonEmpty && (count == 0 || state.matched.exists(_.length + lastRemoved <= current))){
        sub(state.transit(target(current)), current, current + 1, count + 1)
      }else sub(state.transit(target(current)), lastRemoved, current + 1, count)
    }
    sub(automaton)
  }
  object AhoCorasick {
    def apply(patterns: Array[String]): Automaton = {
      val trie = new Trie
      for (word ← patterns) {
        var t = trie
        for (c ← word){
          t.children.find(_._1 == c) match {
            case None ⇒
              val ct = new Trie
              t.children ::= (c, ct)
              t = ct
            case Some(ct) ⇒
              t = ct._2
          }
        }
        t.word = Some(word)
      }
      Automaton.makeFromTrie(trie)
    }
    private class Trie {
      var children: List[(Char, Trie)] = Nil
      var word: Option[String] = None
    }
    object Automaton {
      private[AhoCorasick] def makeFromTrie(trie: Trie): Automaton = {
        val root = new Init()
        val queue: mutable.Queue[(Automaton, Char, Trie)] = mutable.Queue(trie.children.map{case (c, t) ⇒ (root, c, t)}: _*)
        while (queue.nonEmpty){
          val (parent, next, child) = queue.dequeue()
          val failure = takeNext(parent, next)
          val words = child.word.map(_::failure.matched).getOrElse(failure.matched)
          val nextAutomaton = new State(words, failure)
          parent.children(next) = nextAutomaton
          for ((n, t) ← child.children){
            queue.enqueue((nextAutomaton, n, t))
          }
        }
        root
      }
      @tailrec
      private def takeNext(automaton: Automaton, next: Char): Automaton = {
          automaton.children.get(next) match {
            case Some(a) ⇒ a
            case None if automaton.isRoot ⇒ automaton
            case None ⇒ takeNext(automaton.failure, next)
          }
      }
      protected class Init (protected val children: mutable.ListMap[Char, Automaton.State] = mutable.ListMap()) extends Automaton {
        override def isRoot: Boolean = true
        override def transit(char: Char): Automaton = children.getOrElse(char, this)
        override def matched: List[String] = Nil
        override protected def failure: Automaton = this
      }
      protected class State private(protected val children: mutable.ListMap[Char, Automaton.State], private val matchingWords: List[String], protected val failure: Automaton) extends Automaton {
        def this(matchingWords: List[String], failure: Automaton) {
          this(mutable.ListMap(), matchingWords, failure)
        }
        def transit(char: Char): Automaton = {
          children.getOrElse(char, Automaton.takeNext(failure, char))
        }
        def matched = matchingWords
      }
    }
    sealed trait Automaton {
      protected def isRoot: Boolean = false
      def transit(char: Char): Automaton
      def matched: List[String]
      protected def children: mutable.ListMap[Char, Automaton.State]
      protected def failure: Automaton
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}