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 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的更多相关文章

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

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

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

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

  3. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  4. Manthan, Codefest 16 D. Fibonacci-ish

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  5. Manthan, Codefest 16(B--A Trivial Problem)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Manthan, Codefest 16 -A Ebony and Ivory

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

  7. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  8. CF Manthan, Codefest 16 B. A Trivial Problem

    数学技巧真有趣,看出规律就很简单了 wa 题意:给出数k  输出所有阶乘尾数有k个0的数 这题来来回回看了两三遍, 想的方法总觉得会T 后来想想  阶乘 emmm  1*2*3*4*5*6*7*8*9 ...

  9. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

随机推荐

  1. 深入理解ANGULARUI路由_UI-ROUTER

    最近在用 ionic写个webapp 看到几个demo中路由有好几种,搞的有点晕,查下资料研究下,做个笔记,其中大部分为摘抄别人的,做个说明免得被人吐槽. Angularjs ui-router - ...

  2. Git & Gitlab 使用指南

    2016-02-23   |   9,129字   |   分类于 工具  |   3条评论 去年小组在从 SVN 和 TFS 迁移到 Git 的过程中整理了这份文档,面向的用户是对 Git 和 SV ...

  3. JavaScript中一些常用的方法整理

    当前时间和输入时间比较 var timeLong = Date.parse(new Date());//当前时间var t1 = Date.parse($("#returnTime2&quo ...

  4. Java中Properties类知识的总结

    一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...

  5. [flex布局]-flex教程

    简介:2009年,W3C提出了一种新的方案----Flex布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能. Flex布局是什 ...

  6. 使用openvswitch 和dnsmasq来实现虚拟机网络隔离

    openvswicth : 开源的网络虚拟化软件,可以划分vlan隔离虚拟机,做流量控制 dnsmasq:小心的dns,dhcp服务器 安装openvswicth wget  http://openv ...

  7. 高级java必会系列一:多线程的简单使用

    众所周知,开启线程2种方法:第一是实现Runable接口,第二继承Thread类.(当然内部类也算...)常用的,这里就不再赘述.本章主要分析总结线程池和常用调度类. 一.线程池 1.newCache ...

  8. JMeter学习(三十四)测试报告优化

    如果按JMeter默认设置,生成报告如下: 从上图可以看出,结果信息比较简单,对于运行成功的case,还可以将就用着.但对于跑失败的case,就只有一行assert错误信息.(信息量太少了,比较难找到 ...

  9. awk sed 总结

    Awk总结笔记 介绍 90年代 new awk :nawk Linux 的是gawk 我们简化awk 用法 #  awk [options ] ‘scripts’ file1 file2 .... # ...

  10. Memcached,你懂的

    一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...