C. Spy Syndrome 2

题目连接:

http://www.codeforces.com/contest/633/problem/C

Description

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.

Sample Input

30

ariksihsidlihcdnaehsetahgnisol

10

Kira

hates

is

he

losing

death

childish

L

and

Note

Sample Output

Kira is childish and he hates losing

Hint

题意

你有一堆单词,然后把这一堆单词都翻转了,然后拼成了一个串。

然后现在给你一个串,让你找到原来拼的那些单词是什么。

题解:

字典树+dp

直接把那个串翻转一下,就相当于倒着做了嘛

然后我们跑dp就好了,vis[i]表示这个位置能否被转移到

然后我们就开始在字典树上面跑啊跑,看最远能够跑到哪儿,跑到了都打一个vis[i]=1,然后记录一个pre

然后再倒着输出一个就完了。

注意大小写……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
struct node{
int ptr[26] , c ;
};
char s[maxn];
int tot;
int vis[maxn];
int pre[maxn];
node t[maxn];
void init_node(node & f){
memset(f.ptr , 0 , sizeof(f.ptr));
f.c = 0;
}
void init(){
tot = 1 ; init_node(t[0]);
}
inline int GetIdx(char ch){
return ch - 'a';
}
void in(string str)
{
int cur = 0;
for(int i = 0 ; i < str.size() ; ++ i)
{
int idx = GetIdx(str[i]);
if(t[cur].ptr[idx])
cur = t[cur].ptr[idx];
else
{
init_node(t[tot]);
cur = t[cur].ptr[idx] = tot ++ ;
}
}
t[cur].c++;
}
void solve2(int x)
{
int o = x;
x++;
int cur=0,step=0;
while(1)
{
int idx = GetIdx(s[x++]);
if(t[cur].ptr[idx]) cur = t[cur].ptr[idx];
else return;
step ++;
if(t[cur].c)vis[o+step]=1,pre[o+step]=o;
}
}
int n,m;
map<string,string> H;
vector<string>ans;
void solve1()
{
scanf("%d",&m);
while(m--)
{
string s1,s2,s3;
cin>>s1;
for(int i=0;i<s1.size();i++)
{
s2+=s1[i];
if(s1[i]<='Z'&&s1[i]>='A')s1[i]=s1[i]+'a'-'A';
s3+=s1[i];
}
H[s3] = s2;
in(s1);
}
}
int main()
{
init();
scanf("%d%s",&n,s+1);
reverse(s+1,s+1+n);
solve1();
vis[0]=1;
for(int i=0;i<n;i++)
if(vis[i])
solve2(i);
while(n)
{
string tmp;
for(int i=pre[n]+1;i<=n;i++)
tmp+=s[i];
ans.push_back(H[tmp]);
n=pre[n];
}
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
cout<<endl;
}

Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp的更多相关文章

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

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

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

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

  3. Codeforces 633 C Spy Syndrome 2 字典树

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

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

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

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

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

  6. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  7. Manthan, Codefest 16

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

  8. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. HDU5715 XOR 游戏 二分+字典树+dp

    当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...

随机推荐

  1. peewee外键性能问题

    # 转载自:https://www.cnblogs.com/miaojiyao/articles/5217757.html 下面讨论一下用peewee的些许提高性能的方法. 避免N+1查询 N+1查询 ...

  2. 【Windows使用笔记】Windows日常使用软件

    整理一些对于我来说日常使用的Windows软件. 排名不分先后,仅凭我想起来的顺序! 1 MadAppLauncher 这个对我来说非常需要了. 使用它可以快速启动日常常用的软件,非常快捷高效.一般来 ...

  3. 測試 battery capacity curve 的負載

    昨天有同事問說, 他要測試 battery capacity curve, 並且負載要使用 33mA, 於是我想到有一個 apk 名稱為 快速放電 (最下方),可以控制 cpu 的 load, 他試了 ...

  4. freemark学习

    学习地址: http://blog.csdn.net/hejinxu/article/details/6694890   对freemarker的用法与语法进行了详细的讲解 http://freema ...

  5. 生命周期(vue的钩子函数)

    生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...

  6. 让IE6支持css3,让 IE7、IE8 都支持CSS3

    但凡是前端工程师,都知道IE6,IE7,IE8不支持.或者不完全支持CSS3的属性. CSS3 有很多很强大.绚丽的效果,比如,圆角,阴影,渐变透明,渐变背景,等等. 因为IE6时代,没有什么标准,而 ...

  7. P4819 [中山市选]杀人游戏

    题目描述 一位冷血的杀手潜入Na-wiat,并假装成平民.警察希望能在NN个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人,谁是杀手,谁是平民.假如查 ...

  8. P2511 [HAOI2008]木棍分割

    目录 Description Solution Code Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, ...

  9. P3957 跳房子(二分答案+单调队列优化DP)

    题目链接:https://www.luogu.org/contestnew/show/4468 题目大意:跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则 ...

  10. redis之(二十)redis的总结一

    1 什么是Redis Redis(REmote DIctionary Server,远程数据字典服务器)是开源的内存数据库,常用作缓存或者消息队列. Redis的特点: Redis存在于内存,使用硬盘 ...