【Manthan, Codefest 16C】【DP SET-MAP 字典树哈希法】Spy Syndrome 2 字符串是否由字典库单词反转加密而成

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.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e4 + 10, M = 1e6+10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m;
char s[N], t[1010];
int f[N];
string a[N * 10];
int l[N * 10];
map<string, int>mop;
void print(int p)
{
	if (p == 0)return;
	print(p - l[f[p]]);
	cout << a[f[p]] << " ";
}
int b[M][26]; int id;
int o[M];
void ins(char s[],int ord)
{
	int p = 0;
	for (int i = 0; s[i]; ++i)
	{
		int x = s[i] - 'a';
		if (b[p][x] == 0)b[p][x] = ++id;
		p = b[p][x];
	}
	o[p] = ord;
}
int main()
{
	while (~scanf("%d", &n))
	{
		id = 0;
		MS(b, 0);
		MS(o, 0);
		scanf("%s", s + 1);
		scanf("%d", &m);
		for (int i = 1; i <= m; ++i)
		{
			cin >> a[i];
			l[i] = a[i].size();
			for (int j = 0; j < l[i]; ++j)t[j] = tolower(a[i][j]);
			t[l[i]] = 0;
			ins(t, i);
		}
		MS(f, 0); f[0] = -1;
		for (int i = 1; i <= n; ++i)
		{
			int top = min(i, 1000);
			int p = 0;
			for (int j = 0; j < top; ++j)
			{
				int x = s[i - j] - 'a';
				if (b[p][x] == 0)break;
				p = b[p][x];
				if (f[i - j - 1] && o[p])
				{
					f[i] = o[p];
					break;
				}
			}
		}
		print(n);
		puts("");
	}
	return 0;
}
/*
【trick&&吐槽】
1,这道题有一个细节一定要再次警醒!
我在查询一个字符串是否在单词中的时候,
第一次用到的是查看mop[字符串]是否为0,
然而这样子必然会把这个查询字符串也添加入map中,导致map膨胀过大,造成MLE或TLE
比赛的时候就是因为这个浪费了15mins与增加了2次罚时。

2,用string存字符串就可以
用一个长字符串分开存多个小字符串的方法自然也是有优化作用的,然而优化程度却并不大。

3,这道题其实我们直接把加密后的字符串反转就可以了。。。

4,字符串哈希可以不用map,而用字典树,就可以减少一个log时间

【题意】
有一个长度为n(1<=n<=1e4)的加密字符串
这个字符串是由一个若干单词穿成的句子,经过以下的规则加密而成的。

1,单词来源于单词库,单词库会告诉你。
这个单词库内会有m(1<=m<=1e5)个单词,
每个单词的长度都不超过1e3,所有单词的总长度不超过1e6.
2,我们先会把所有选出的单词连乘串(同一个单词可以选择多次),两两之间一个空格
3,对于所有的单词做字符串reverse操作(即前后倒置)。
4,去掉所有的空格
5,转化为小写

现在已知加密字符串和单词库。
让你还原加密前的句子。

【类型】
DP SET-MAP

【分析】
我们可以把所有的单词都倒过来,再转化为小写,加入SET-MAP中
然后我们再枚举一个字符串的尾端点,向前拓展,记录最后一个延展的字符串编号。
这样这道题就做完了。

用map的话,时间复杂度可达O( n*1000 *log(n*1000) )
用trie之后,时间复杂度是O(n*1000)

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值