Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Separate String

You are given a string $t$ and a set $S$ of $N$ different strings. You need to separate $t$ such that each part is included in $S$.

For example, the following 4 separation methods satisfy the condition when $t = abab$ and $S = \{a, ab, b\}$.

$a,b,a,b$

$a,b,ab$

$ab,a,b$

$ab,ab$

Your task is to count the number of ways to separate $t$. Because the result can be large, you should output the remainder divided by $1,000,000,007$.

Input

The input consists of a single test case formatted as follows.

$N$
$s_1$
:
$s_N$
$t$

The first line consists of an integer $N$ ($1 \leq N \leq 100,000$) which is the number of the elements of $S$. The following $N$ lines consist of $N$ distinct strings separated by line breaks. The $i$-th string $s_i$ represents the $i$-th element of $S$. $s_i$ consists of lowercase letters and the length is between $1$ and $100,000$, inclusive. The summation of length of $s_i$ ($1 \leq i \leq N$) is at most $200,000$. The next line consists of a string $t$ which consists of lowercase letters and represents the string to be separated and the length is between $1$ and $100,000$, inclusive.

Output

Calculate the number of ways to separate $t$ and print the remainder divided by $1,000,000,007$.

Sample Input 1

3
a
b
ab
abab

Output for Sample Input 1

4

Sample Input 2

3
a
b
c
xyz

Output for Sample Input 2

0

Sample Input 3

7
abc
ab
bc
a
b
c
aa
aaabcbccababbc

Output for Sample Input 3

160

Sample Input 4

10
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Output for Sample Input 4

461695029

Solution in Scala (judge verdict: Accepted)

import Main.AhoCorasick.Automaton

import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn

object Main extends App {
  import scala.io.StdIn._

  val file = StdIn

  val n = file.readLine.toInt
  val patterns = Array.tabulate(n){_ ⇒ file.readLine.trim}
  val target = file.readLine.trim
  println(solve(target, patterns))
  def solve(target: String, patterns: Array[String]): Long = {
    val mod = 1000000007L
    val aho = AhoCorasick(patterns)
    val memo = Array.tabulate(target.length + 1){_ ⇒ 0L}.also(_(0) = 1L)
    val automaton = aho.makeAutomaton
    for (i ← target.indices){
      automaton.move(target(i))
      for (length ← automaton.matchLength){
        memo(i + 1) += memo(i - length + 1)
      }
      memo(i + 1) %= mod
    }
    memo.last
  }

  object AhoCorasick {
    private object Trie{
      def apply(patterns: Iterable[String]): Trie = {
        val res = new Trie
        for (pattern ← patterns) {
          var length = 0
          var current = res
          while (length != pattern.length) {
            current.child.find(_._1 == pattern(length)) match {
              case Some((_, trie)) ⇒
                current = trie
                length += 1
              case None ⇒
                val next = new Trie
                current.child ::= (pattern(length), next)
                current = next
                length += 1
            }
          }
          current.word = pattern
        }
        res
      }
    }

    private class Trie {
      private var word: String = ""
      private[AhoCorasick] def pattern: String = word
      private var child: List[(Char, Trie)] = Nil
      private[AhoCorasick] def children: List[(Char, Trie)] = child
    }
    def apply(patterns: Iterable[String]): AhoCorasick = {
      val trie = Trie(patterns)
      val root = new AhoCorasick().also{aho ⇒
        aho.pattern = trie.pattern
      }
      val queue: mutable.Queue[(AhoCorasick, Trie)] = mutable.Queue((root, trie))
      while (queue.nonEmpty){
        val (aho, trie) = queue.dequeue()
        for ((c, t) ← trie.children){
          val next = new AhoCorasick().also{it ⇒
            it.pattern = t.pattern
            val (f, m) = findNext(aho, c)
            it.failure = f
            it.matchLength = if (it.hasPattern) t.pattern.length::m else m
          }
          aho.child ::= (c, next)
          queue.enqueue((next, t))
        }
      }
      root
    }
    @tailrec
    private[this] def addToResult(current: AhoCorasick, length: Int, result: List[(Int, Int)]): List[(Int, Int)] = {
      if (current.isRoot) result
      else if (current.hasPattern) addToResult(current.failure, length, (length - current.patternLength, length - 1)::result)
      else addToResult(current.failure, length, result)
    }
    @tailrec
    private[this] def findNext(current: AhoCorasick, next: Char): (AhoCorasick, List[Int]) = {
      current.child.find(_._1 == next) match {
        case Some((_, t)) ⇒ (t, t.matchLength)
        case None if current.isRoot ⇒ (current, Nil)
        case _ ⇒ findNext(current.failure, next)
      }
    }
    @tailrec
    private[this] def findNext(current: AhoCorasick, target: String, length: Int): AhoCorasick = {
      current.child.find(_._1 == target(length)) match {
        case Some((_, r)) ⇒ r
        case None if current.isRoot ⇒ current
        case _ ⇒ findNext(current.failure, target, length)
      }
    }
    @tailrec
    private def findAll(current: AhoCorasick, target: String, length: Int, result: List[(Int, Int)]): List[(Int, Int)] = {
      if (target.length == length) addToResult(current, length, result)
      else findAll(findNext(current, target, length), target, length + 1, addToResult(current, length, result))
    }
    class Automaton private[AhoCorasick] (private var aho: AhoCorasick){
      def move(char: Char): Unit = {
        var isEnd = true
        while(isEnd) {
          aho.child.find(_._1 == char) match {
            case Some((_, next)) ⇒
              aho = next
              isEnd = false
            case None if aho.isRoot ⇒
              isEnd = false
            case None ⇒ aho = aho.failure
          }
        }
      }
      def matchLength: List[Int] = aho.matchLength
    }
  }
  class AhoCorasick {
    private var failure = this
    private var pattern = ""
    private var matchLength: List[Int] = Nil
    private var child = Nil: List[(Char, AhoCorasick)]
    private def isRoot: Boolean = failure == this
    private def hasPattern: Boolean = pattern != ""
    private def patternLength: Int = pattern.length
    def findAllMatch(target: String): List[(Int, Int)] = {
      AhoCorasick.findAll(this, target, 0, Nil)
    }
    def makeAutomaton: Automaton = new Automaton(this)
  }

  implicit class Scope[T](val value: T) extends AnyVal{
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](func: T ⇒ A): A = func(value)

  }
}