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
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.
我的思路是:将每一组正确的单词按倒序建立一颗字典数,然后直接查找text中是否该单词存在,存在就输出。。
值得注意的是这样的样例:
7
kirkirk
2
rik
krik
如果先输出krik的话剩下的就没有单词匹配了,所以我们每一次匹配单词成功时都需要判断 从当前i+1位以后 是否还能匹配,能的话直接输出当前匹配的单词,不能的话说明 如果我们当前匹配到第 i 位 那么剩下的单词就无法完成匹配了,所以正确的匹配位置应该在第 i 位之前 ,重新找一下就好。。。
代码如下:
#include<bits/stdc++.h>
using namespace std;
map<string,string>mp;
int k=0;
int c=0;
typedef struct Trie
{
Trie *next[26];
int sum;
};
Trie *root;
void init()
{
root=new Trie;
for(int i=0; i<26; i++)
{
root->next[i]=NULL;
}
}
void createTrie(string num)
{
int len=num.length();
Trie *p=root;
for(int i=0; i<len; i++)
{
int id=(int)(num[i]-'a');
if(p->next[id]==NULL)
{
p->next[id]=new Trie;
p=p->next[id];
p->sum=0;
for(int j=0; j<26; j++)
{
p->next[j]=NULL;
}
}
else
{
p=p->next[id];
}
}
p->sum=1;
}
bool fin2(char *str,int h)
{
int len=strlen(str);
Trie *p=root;
for(int i=h;i<len;i++)
{
int id=(int)(str[i]-'a');
if(p->next[id]!=NULL)
{
p=p->next[id];
if(p->sum==1)return true;
}
else return false;
}
return false;
}
void fin(char str[],int h)
{
int len=strlen(str);
Trie *p=root;
string stt;
for(int i=h; i<len; i++)
{
int id=(int)(str[i]-'a');
if(p->next[id]!=NULL)
{
p=p->next[id];
stt+=id+'a';
k++;
}
else break;
}
if(fin2(str,k)||k==len)
cout<<mp[stt];
else
{
Trie *p=root;
stt.clear();
int i;
for(i=h;i<k-1;i++)
{
int id=(int)(str[i]-'a');
if(p->next[id]!=NULL)
{
p=p->next[id];
stt+=id+'a';
if(p->sum==1&&fin2(str,i+1))
{
k=i+1;
break;
}
}
else break;
}
cout<<mp[stt];
}
}
int main()
{
int n,t;
init();
char ss[10005];
scanf("%d",&n);
scanf("%s",ss);
scanf("%d",&t);
string nu;
char s[1005];
for(int i=0; i<t; i++)
{
nu.clear();
scanf("%s",s);
for(int j=strlen(s)-1; j>=0; j--)
{
if(s[j]>='A'&&s[j]<='Z')
{
nu+=s[j]+32;
}
else
{
nu+=s[j];
}
}
createTrie(nu);
mp[nu]=s;
}
while(k<n)
{
if(c==1)
{
printf(" ");
}
fin(ss,k);
c=1;
}
printf("\n");
}
/*
7
kirkirk
2
rik
krik
*/