This page is still under construction.

Parts of this page are still being built. What you see may change.

Messy kangaroo babies

Time limit5sMemory limit1024 MB

Summary
Given a word S and a list of candidate synonyms, count how many are subsequences of S that can be embedded in at least two different ways.
Level

Hard8 of 10

Topics
String, Dynamic programming, Greedy, Binary search
Solved
No attempts yet

Problem

A kangaroo word is a word that carries a synonym of itself (a "baby") in such a way that every letter of the synonym appears in the word, in the same order. For example, pastej is a kangaroo word, because it carries the synonym paj (pastej). aste and atj would also count as babies if we pretend they are words, but paaj and etsa would not. Formally, the baby must be a subsequence of the word.

Furthermore, we say a baby is messy if it fits in the word in two different ways. paj is not a messy baby, but if the original word had been paastej, it would be, since it could then be hidden as either paastej or paastej.

Given a (made-up) word SS and a list of (made-up) synonyms, how many of the synonyms are messy babies of SS?

Input

  • The first line contains a non-empty string consisting of the letters a-z, the word SS we are asking about.
  • The second line contains the integer NN (1≤N≤100 0001 \le N \le 100\,000): the number of synonyms of the word.
  • The following NN lines contain the synonyms, each a non-empty string consisting of the letters a-z.

No synonym will appear twice, or be equal to SS.

Let MM denote the number of letters in SS, and KK the sum of the number of letters in the synonyms. Then M≤100 000M \le 100\,000 and K≤500 000K \le 500\,000.

Output

Print a single integer: the number of words that are messy babies of SS.

Hint

In sample 1, the first three words are babies of SS, and also messy. The test case could therefore appear in test group 2 or 4.

In sample 2, the first four words are babies, of which the first two are also messy babies. This test case could not appear in test group 2 or 4.

Examples2

  1. Example 1

    Input
    paastej
    5
    paj
    aste
    atj
    paaaj
    etsa
    
    Expected output
    3
    
  2. Example 2

    Input
    ababa
    6
    aa
    aba
    abb
    baa
    aabb
    xyz
    
    Expected output
    2