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.
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.
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.
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Kira is childish and he hates losing
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
HI there HeLLo
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
思路:暴力,字典树+DFS。感觉这题有点水过去的感觉。
说下我的思路,就是将下面给的单词到过来存进字典树中,然后用DFS正着搜。时间给了2s,140ms过的,也差点爆了内存
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 typedef long long LL; 10 char str[10005]; 11 char trr[10005]; 12 char strr[1005]; 13 char tt[100005][1001]; 14 int BB; 15 struct node 16 { 17 node*p[26]; 18 int id;//记录单词的标号 19 node() 20 { 21 id=-2; 22 memset(p,0,sizeof(p)); 23 } 24 }; 25 int N=0; 26 int ii[100005]; 27 int rr[100005]; 28 node *head=new node(); 29 void inserts(struct node*r,char *q,int v)//字典树的添加 30 { 31 int i,j,l; 32 node*NN=r; 33 l=strlen(q); 34 for(i=l-1; i>=0; i--) 35 { 36 int s=tolower(q[i])-'a'; 37 if(NN->p[s]==NULL) 38 { 39 node*n=new node(); 40 NN->p[s]=n; 41 NN=n; 42 } 43 else 44 NN=NN->p[s]; 45 if(i==0) 46 NN->id=v; 47 } 48 } 49 struct node*ask(char p,struct node*r)//查询,返回当前的节点 50 { 51 int i,j; 52 struct node*dd=r; 53 int s=p-'a'; 54 return dd->p[s]; 55 } 56 void dfs(int id,int l,int k,node*d) 57 { 58 if(N)return ; 59 int i; 60 char tr[10005]; 61 if(id==l&&N==0) 62 {for(i=0;;i++) 63 { 64 if(ii[i]) 65 { 66 rr[i]=ii[i]; 67 } 68 else break; 69 } 70 N=1; 71 return ; 72 }node *WW; 73 for(i=id; i<l; i++) 74 { 75 if(N)return ; 76 tr[i-id]=str[i]; 77 tr[i-id+1]='