Manthan, Codefest 16 -C. Spy Syndrome 2
来源:互联网 发布:淘宝买家秀木耳图链接 编辑:程序博客网 时间:2024/06/05 10:02
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 n lowercase 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
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
output
Kira is childish and he hates losing
input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
output
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.
字典树的应用,以单词建树,将密码串反向记忆化搜索
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <iostream>#include <algorithm>using namespace std;const int Max = 1e6+10;const int MaxM = 10010;typedef struct node{ int next[30]; int mark;}Tree;Tree Tr[Max];int top;char str[Max];char s[MaxM*10][1011];int n,m;int vis[MaxM];bool flag;int NewNode(){ for(int i=0;i<=26;i++) { Tr[top].next[i] = -1; } Tr[top].mark = -1; return top++;}int ok(char c) //大小写转换{ if(c>='a'&&c<='z') { return c-'a'; } else { return c-'A'; }}void Build(int Root,int index){ int len = strlen(s[index]); for(int i = 0; i < len ;i++) { int ans = ok(s[index][i]); if(Tr[Root].next[ans]==-1) { Tr[Root].next[ans] = NewNode(); } Root = Tr[Root].next[ans]; } Tr[Root].mark = index;}int DFS(int pos){ if(pos==-1) { return true; } if(vis[pos]!=-1)//判断是否之前是否遍历到 { return vis[pos]; } int Root = 0; for(int i = pos;i>=0;i--) { int ans = ok(str[i]); if(Tr[Root].next[ans]==-1) { break; } Root = Tr[Root].next[ans]; if(Tr[Root].mark!=-1&&DFS(i-1)) { if( flag ) printf(" "); else flag = true; printf("%s", s[Tr[Root].mark]); return vis[pos] = 1; } } return vis[pos] = 0;}int main(){ scanf("%d",&n); scanf("%s",str); scanf("%d",&m); top = 0;flag = false; int Root = NewNode(); for(int i=0;i<m;i++) { scanf("%s",s[i]); Build(Root,i);//建立字典树 } for(int i = 0 ; i<MaxM;i++) { vis[i] = -1; } DFS(n-1);//记忆化搜索 return 0;}
- Manthan, Codefest 16 -C. Spy Syndrome 2
- Manthan, Codefest 16 633C Spy Syndrome 2(dfs + stl)
- Manthan, Codefest 16 C. Spy Syndrome 2 ★
- Manthan, Codefest 16 C. Spy Syndrome 2(dp、trie|哈希)
- Manthan, Codefest 16 (C. Spy Syndrome 2 (trie树 + DP))
- Manthan, Codefest 16 C. Spy Syndrome 2(Trie树套DP)
- 【Manthan, Codefest 16C】【DP SET-MAP 字典树哈希法】Spy Syndrome 2 字符串是否由字典库单词反转加密而成
- CodeForces 633C Spy Syndrome 2
- CF633C Spy Syndrome 2
- Manthan Codefest 16 解题报告
- Codeforces 633C Spy Syndrome 2(Trie+dp)
- Codeforces 633C Spy Syndrome 2 【字典树 + DFS】
- codeforces 633C. Spy Syndrome 2 trie + dp
- Codeforces 633C Spy Syndrome 2(Trie+暴力)
- Codeforces 633C Spy Syndrome 2 字典树
- Manthan, Codefest 16 -A Ebony and Ivory
- Manthan, Codefest 16 -B. A Trivial Problem
- Manthan, Codefest 16(A)暴力
- Spring Task
- Android Studio运行项目报错:java.lang.UnsatisfiedLinkError——.so文件问题
- Hibernate--- list和iterator方法的区别
- TCPIP三次握手与四次挥手
- Python-----生成器:generator
- Manthan, Codefest 16 -C. Spy Syndrome 2
- 一、 Ubuntu12.04的安装和配置
- MBProgressHUD的基本使用
- 持续集成(CI)、自动化构建和自动化测试--初探
- 谈谈Android中的Divider
- leetcode-- Valid Sudoku
- 数据结构常见面试(1)
- SQL Server 中Inner join 和where的效率差异
- Java类加载器(自定义类加载器)