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.

思路:暴力,字典树+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]='\0';
78 if(i==id)WW=head;
79 node *nn=ask(tr[i-id],WW);
80 if(nn==NULL)//如果没有符合的直接跳出
81 {
82 return ;
83 }
84 if(nn->id!=-2)//当符合了进入下一层
85 {
86 ii[k]=nn->id;
87 dfs(i+1,l,k+1,nn);
88 ii[k]=0;
89 } WW=nn;//继续按当前的前缀串查找
90 }
91 }
92 int main(void)
93 {
94 int i,j,k,s;
95 N=0,BB=0;
96 scanf("%d",&k);
97 scanf("%s",str);
98 scanf("%d",&s);
99 int ans=0;
100 for(i=1; i<=s; i++)
101 {
102 scanf("%s",tt[i]);
103 int l=strlen(tt[i]);
104 inserts(head,tt[i],i);
105 }
106 dfs(0,k,0,head);
107 for(i=0;;i++)
108 {
109 if(rr[i]==0)
110 {
111 break;
112 }
113 }
114 int sl=i-1;
115 printf("%s",tt[rr[0]]);
116 for(i=1; i<=sl; i++)
117 {
118 printf(" %s",tt[rr[i]]);
119 }
120 return 0;
121 }

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

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

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

  2. codeforces 633C. Spy Syndrome 2 hash

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

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

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

  4. Codeforces 633C Spy Syndrome 2(DP + Trie树)

    题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...

  5. Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】

    <题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...

  6. [codeforces] 633C Spy Syndrome 2

    原题 Trie树+dp 首先,我们可以简单的想到一种dp方式,就是如果这一段可以匹配并且可以与前一段接上,那么更新dp[i]为当前字符串的编号,然后倒推就可以得到答案. 但是,显然我们不能O(m)比较 ...

  7. Codeforce 633.C Spy Syndrome 2

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

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

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

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

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

随机推荐

  1. android studio Please configure Android SDK / please select Android SDK

    有可能是sdk文件损坏造成的. file->settings->appearance&behavior->system settings->android sdk-&g ...

  2. 日常Java 2021/10/4

    读取控制台输入 将System.in包装在BufferedReader对象中来创建一个字符流 BufferedReader b = new BufferedReader(new InputStream ...

  3. vmware使用nat连接配置

    一.首先查看自己的虚拟机服务有没有开启,选择电脑里面的服务查看: 1.计算机点击右键选择管理  2.进入管理选择VM开头的服务如果没有开启的话就右键开启  二.虚拟机服务开启后就查看本地网络虚拟机的网 ...

  4. Swift Storyboard找不到类文件

    Swift语言引入了Module概念,在通过关键字@objc(类名)做转换的时候,由于Storyboard没有及时更新Module属性,会导致如下两种类型错误: 1 用@objc(类名)标记的Swif ...

  5. spring-dm 一个简单的实例

    spring-dm2.0  运行环境,支持JSP页面 运行spring web 项目需要引用包

  6. 【C#】【MySQL】【GridView】删除出现Parameter index is out of range

    [编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...

  7. [手写系列] Spirit带你实现防抖函数和节流函数

    前言 防抖函数和节流函数,无论是写业务的时候还是面试的时候,想必大家已经听过很多次了吧.但是大家在用到的时候,有了解过他们之间的区别嘛,他们是如何实现的呢?还是说只是简单的调用下像lodash和und ...

  8. 人工水母搜索算法--JS

    1. 人工水母算法原理背景 水母生活在世界上不同深度和温度的水中.它们酷似钟状,一些水母的直径小于1cm,然有些水母直径则非常大.它们有各种各样的颜色.大小和形状.大多数水母偏好海洋环境.它们进食的方 ...

  9. [BUUCTF]REVERSE——CrackRTF

    CrackRTF 附件 步骤: 例行查壳儿,32位程序,无壳儿 32位ida载入,main函数开始分析程序 破解第一个密码 sub_40100A()是一个加密函数,具体的写的算法没去分析,但是Cryp ...

  10. BUUCFT pwn asis2016_b00ks

    看师傅们wp的时候,我才知道这个道题是wiki上面的例题.我看了一些师傅的wp,发现大家都是一种做法,都是通过mmap堆地址,来找libc基地址的.而我试了一下fastbisn attack,发现也可 ...