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
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.

题目大意:将一个字符串加密的规则:先将所有字母变成小写字母,再将每个单词翻转,拼接在一起.现在给出可能用到的单词,还原字符串.

分析:既然题干中说所有的单词都翻转过来了,那么就把它给出的单词全部翻转过来.之后就有点像是在一个字典中查询单词有没有出现过这种操作,利用trie.因为n不大,在匹配加密串的时候可以用搜索:固定起点,枚举终点,每次看在trie中能不能找到结尾标记以及能不能走下去.翻转操作可以变成倒着插入trie.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, m, tot = , cnt, ans[], len[];
char s[][], s2[]; struct node
{
int tr[];
int id;
}e[]; void insert(char *ss, int x)
{
int len = strlen(ss);
int u = ;
for (int i = len - ; i >= ; i--)
{
char ch = ss[i];
if (ch < 'a' || ch > 'z')
ch += 'a' - 'A';
int p = ch - 'a';
if (!e[u].tr[p])
e[u].tr[p] = ++tot;
u = e[u].tr[p];
}
e[u].id = x;
} void solve(int dep)
{
if (dep == n + )
{
for (int i = ; i < cnt; i++)
cout << s[ans[i]] << " ";
cout << s[ans[cnt]] << endl;
exit();
}
int u = ,i;
for (i = dep; i <= n; i++)
{
int p = s2[i] - 'a';
if (!e[u].tr[p])
break;
u = e[u].tr[p];
if (e[u].id)
{
ans[++cnt] = e[u].id;
solve(dep + len[e[u].id]);
--cnt;
}
}
} int main()
{
scanf("%d", &n);
scanf("%s", s2 + );
scanf("%d", &m);
for (int i = ; i <= m; i++)
{
scanf("%s", s[i]);
len[i] = strlen(s[i]);
insert(s[i], i);
}
solve(); return ;
}

Codeforce 633.C Spy Syndrome 2的更多相关文章

  1. Codeforces 633 C Spy Syndrome 2 字典树

    题意:还是比较好理解 分析:把每个单词反转,建字典树,然后暴力匹配加密串 注:然后我就是特别不理解,上面那种能过,而且时间很短,但是我想反之亦然啊 我一开始写的是,把加密串进行反转,然后单词正着建字典 ...

  2. Codeforce 633C. Spy Syndrome 2

    C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

  4. Manthan, Codefest 16 -C. Spy Syndrome 2

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. codeforces 633C. Spy Syndrome 2 hash

    题目链接 C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. Codeforces 633C Spy Syndrome 2 | Trie树裸题

    Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...

  7. CF#633C Spy Syndrome 2 DP+二分+hash

    Spy Syndrome 2 题意 现在对某个英文句子,进行加密: 把所有的字母变成小写字母 把所有的单词反过来 去掉单词之间的空格 比如:Kira is childish and he hates ...

  8. CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie

    题目链接:http://codeforces.com/problemset/problem/633/C 大意就是给个字典和一个字符串,求一个用字典中的单词恰好构成字符串的匹配. 比赛的时候是用AC自动 ...

  9. CF633C:Spy Syndrome 2——题解

    https://vjudge.net/problem/CodeForces-633C http://codeforces.com/problemset/problem/633/C 点击这里看巨佬题解 ...

随机推荐

  1. 从零开始的Python学习Episode 9——集合

    集合 集合是一个无序的,不重复的数据组合,是python基本的数据类型,把不同的元素组成一起就形成集合. 一.创建集合 s = set('smile')list = ['1','2','3']prin ...

  2. Wacom将在CES 2015上发布全新旗舰版Cintiq

    Cintiq 27QHD和Cintiq 27QHD touch拥有宽大的工作表面,以及令人惊喜的屏幕笔触和颜色性能. 2015年1月6日,Wacom发布了Cintiq 27QHD和Cintiq 27Q ...

  3. Powershell按文件最后修改时间删除多余文件

    Powershell按文件最后修改时间删除多余文件 1. 删除目录内多余文件,目录文件个数大于$count后,按最后修改时间倒序排列,删除最旧的文件. Sort-Object -Property La ...

  4. 使用 MPI for Python 并行化遗传算法

    前言 本文中作者使用MPI的Python接口mpi4py来将自己的遗传算法框架GAFT进行多进程并行加速.并对加速效果进行了简单测试. 项目链接: GitHub: https://github.com ...

  5. Scrum立会报告+燃尽图(Beta阶段第七次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2388 项目地址:https://coding.net/u/wuyy694 ...

  6. 20181016-4 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 05

    作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 Scrum master:王硕 一.小组介绍 组长:王一可 组员:范 ...

  7. Dijkstra 最短路径算法 秒懂详解

    想必大家一定会Floyd了吧,Floyd只要暴力的三个for就可以出来,代码好背,也好理解,但缺点就是时间复杂度高是O(n³). 于是今天就给大家带来一种时间复杂度是O(n²),的算法:Dijkstr ...

  8. TensorFlow:NameError: name ‘input_data’ is not defined

    在运行TensorFlow的MNIST实例时,第一步 import tensorflow.examples.tutorials.mnist.input_data mnist = input_data. ...

  9. struts2--上传总结(限制大小和类型 非法上传的跳转)

    网上有很多版本,鉴于实践出真知的态度 我自己探索了一番 struts版本:2.3.16 限制大小: struts2默认是2M 所以如果要扩大大小限制,应该先配一个全局struts2最大上限 <c ...

  10. ranch代码简述

    最近要看一下erlang连接池,觉得ranch很不错. github上面有人写了ranch的代码阅读,可以看一下,链接在这里. 1. ranch可以同时监听多个端口,每个端口的连接信息可以单独配置. ...