#P3121. [USACO15FEB] Censoring G

[USACO15FEB] Censoring G

Description

FJ copied all the articles from a magazine and turned them into a string ss of length at most 10510^5. He has a list of nn words, denoted by t1tnt_1 \cdots t_n. He wants to delete these words from ss.

Each time, FJ finds in ss the earliest occurrence of any word from the list (the earliest occurrence means the smallest starting position), and then deletes that word from ss. He repeats this operation until no listed word remains in ss. Note that deleting a word may cause another listed word to appear in ss.

FJ noticed that within the list, no word is a substring of another, which implies that the starting positions of occurrences of listed words in ss are pairwise distinct.

Please help FJ perform these operations and output the final ss.

Input Format

The first line contains a string, the article ss.

The second line contains an integer, the number of words in the list nn.

Lines 33 through (n+2)(n + 2) each contain a string; the string on line (i+2)(i + 2), tit_i, denotes the ii-th word.

Output Format

Output a single line containing the string ss after all operations.

begintheescapexecutionatthebreakofdawn 
2 
escape 
execution 

beginthatthebreakofdawn 

Hint

Constraints

For all test points, it is guaranteed that:

  • $1 \leq |s|, \sum\limits_{i = 1}^n |t_i|, n \leq 10^5$.
  • All strings contain only lowercase letters.
  • After the operations finish, ss will not become an empty string.
  • For all iji \neq j, tit_i is not a substring of tjt_j.

Here, for a string xx, x|x| denotes its length.


Hint

During the process, a prefix of ss may be completely deleted; pay special attention to this.

Translated by ChatGPT 5