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:
- 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.
题意: 一个密码,由很多单词分别进行操作后,连接而成。操作如下 :
先翻转,然后全部变为小写字母,最后消去单词间空格,连接成密码。
现给你一本字典和一个密码,求该密码的原意。
分析:反向建立字典树即可,val保存每一个单词的下标,用dfs暴力的解决每一个字母的归属
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 1000000+5
#define MAX 26
typedef long long ll;
const int maxnode=1000000+100;//预计字典树最大节点数目
const int sigma_size=27; //每个节点的最多儿子数
string s1;
string tmp[N];
int ans[N];
int n;
struct Trie
{
int ch[maxnode][sigma_size];//ch[i][j]==k表示第i个节点的第j个儿子是节点k
int val[maxnode];//val[i]==x表示第i个节点的权值为x
int sz;//字典树一共有sz个节点,从0到sz-1标号
//初始化
void clear()
{
sz=1;
memset(ch,0,sizeof(ch));//ch值为0表示没有儿子
memset(val,0,sizeof(val));
}
//在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
//所以insert前需要判断一下是否已经存在s单词了
void insert(string s,int j)
{
int u=0,n=s.length();
for(int i=n-1;i>=0;i--)///建立字典树
{
s[i]=tolower(s[i]);
int id=s[i]-'a';
if(ch[u][id]==0)//无该儿子
{
ch[u][id]=sz;
memset(ch[sz],0,sizeof(ch[sz]));
sz++;
}
u=ch[u][id];
}
val[u]=j;
}
};
Trie trie;
int tot=0;
int flag=0;
bool dfs(int x)
{
if(x==n)
{
for(int i=0;i<tot;i++)
{
cout<<tmp[ans[i]]<<" ";
}
cout<<endl;
flag=1;
return 1;
}
if(flag==1) return 1;
int pos=0;
for(int i=x;i<n;i++)
{
int id=s1[i]-'a';
pos=trie.ch[pos][id];
//cout<<pos<<endl;
if(pos==0) return 0;///如果该分支子杰节点无该字母
// cout<<x<<" "<<endl;
if(trie.val[pos])///判读是不是单词结尾
{
ans[tot++]=trie.val[pos];
if(dfs(i+1))
{
return 1;
}
tot--;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
trie.clear();
cin>>s1;
int m;
scanf("%d",&m);
tot=0;
flag=0;
for(int i=1;i<=m;i++)
{
cin>>tmp[i];
trie.insert(tmp[i],i);
}
dfs(0);
}
return 0;
}