Codeforces C. Spy Syndrome 2 (字典树+DFS)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86524152

C. Spy Syndrome 2

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

  1. Convert all letters of the sentence to lowercase.
  2. Reverse each of the words of the sentence individually.
  3. 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

Copy

30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note

output

Copy

Kira is childish and he hates losing 

input

Copy

12
iherehtolleh
5
HI
Ho
there
HeLLo
hello

output

Copy

HI there HeLLo 

Note

In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.

题目大意:给你一个字符串s,它是由接下来输入的m个字符串组成的,但是是由那m个字符串都转换小写字母,并且每一个都颠倒顺表拼接成的,让你求出按照那m个字符串的形式还原出原来的字符串。具体看样例应该可以明白。

解题思路:把那m个字符串用字典树存起来,不过存的时候倒着存,因为给出的s串是由m个串颠倒组成的。然后对于字符串s从头开始在字典树上作dfs搜索就行了,每次搜到一个单词,用数组把下表保存下来最后输出即可。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e5 +10 ;
const int Max = 26;

struct Tire{
    Tire *Next[Max] ;
    int index ;
    Tire(){
        memset(Next, NULL, sizeof(Next)) ;
        index = -1 ;
    }
};

int n, m, flag ;
string s1 ,s2[Maxn] ;
Tire *root ;
int indexing[Maxn], re[Maxn] ;

void insert (string s, int k){
    Tire *p = root ;
    int len = s.size() ;
    transform(s.begin(), s.end(), s.begin(), ::tolower) ;
//    cout << s << endl ;
    for (int i = len - 1; i >= 0; i--){
        int id = s[i] - 'a' ;
        if (p -> Next[id] == NULL) p -> Next[id] = new Tire() ;
        p = p -> Next[id] ;
        if (i == 0) p -> index = k ;
    }
}

void Dfs (int key, int len, int sub){
    char s[Maxn] ;
    if (flag) return ;
    if (key == len && !flag){
        for (int i = 0; ;i++) {
            if (indexing[i]) re[i] = indexing[i] ;
            else break ;
        }
        flag = 1 ;
        return ;
    }
    Tire *q ;
    for (int i = key; i < len; i++){
        if (flag) return ;
        s[i - key] = s1[i] ;
        if (i == key) q = root ;
        int id = s[i - key] - 'a' ;
        Tire *tmp = q -> Next[id] ;
        if (tmp == NULL) return ;
        if (tmp -> index != -1){
            indexing[sub] = tmp -> index ;
            Dfs(i + 1, len, sub + 1) ;
            indexing[sub] = 0 ;
        }
        q = tmp ;
    }
}

int main (){
    root = new Tire() ;
    cin >> n >> s1 >> m ;
    for (int i = 1; i <= m; i++){
        cin >> s2[i] ;
        insert(s2[i], i) ;
    }
    Dfs(0, n, 0) ;
    int up = -1 ;
    for (int i = 0; ; i++){
        if (!re[i]) {
            up = i ;
            break ;
        }
    }
//    cout << up << endl ;
    cout << s2[re[0]] ;
    for (int i = 1; i <= up; i++){
        cout << " " << s2[re[i]] ;
    }
    cout << endl ;
    return 0 ;
}

古今多少事

都付笑谈中

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86524152