After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can’t use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
- Convert all letters of the sentence to lowercase.
- Reverse each of the words of the sentence individually.
- Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of nlowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It’s guaranteed that the total length of all words doesn’t exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
input
| |
output
| |
input
| |
output
| |
Note
In sample case 2 there may be multiple accepted outputs, “HI there HeLLo” and “HI there hello” you may output any of them.
Solution
题目大意:给你一个删除了空格,整个字符串翻转之后,全部变为小写的字符串\(S\),和许多单词\(W_i\),你要找出合法的一个原串。
(这道题我考场上想的是AC自动机…)
我们可以先把单词翻转,建立一颗Trie树,然后枚举\(S\)的所有子串,如果该子串跑完Trie之后的节点为一个结束状态\(now\),这个位置为\(pos\)时,那么我们记录\(ans_{pos}\),表示这个位置的字符串可以对应第\(ans_{pos}\)个单词,记录\(last_{pos}\),表示这个单词可以匹配\(S_{last_{pos+1}, pos}\)。
考虑答案的时候,从位置\(n\)开始跑一边dfs,每次输出这个单词,然后跳转到\(last_n\)上就可以了。
Code
| |