CF633C Spy Syndrome 2
题面:把一句话加密:1.所有字母变成小写。2.翻转所有单词。3.去掉空格。然后给你一句加密后的字符串以及一些出现在原句和没有出现在原句的单词,让你还原原句。注意,每一个单词可以使用多次,如果有多个答案,输出其中任意一个。
trie树好题……
首先都能想到的是把所有单词建成一棵trie树,然后我是这么想的,对于加密后字符串,每句每一个字符作为单词结尾,然后倒着再trie树上跑,知道遇到一个单词,记录是第几个,最后输出。
但是这样是不对的,因为无法保证遇到一个单词后,这个单词之前的部分已经匹配好了,举个例子:High,加密后是hgih,然后给定的单词有 hi 和 high,当扫到最后一个字符'h'的时候,在trie树上得到的第一个单词是 hi,而hi之前的gh无法构成一个单词,所以gg。
因此,我们还要有一个pre数组,记录每一个匹配点的上一个是由哪一个匹配点转移过来的,在trie树上跑的时候,只有当前点构成一个单词,且这个单词的前一个字符是匹配点,才return。
最后沿着pre数组找到头,倒叙输出。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e4 + ;
const int maxm = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = ans * + ch - '', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
char s[maxn], ss[maxm][]; int cnt = ;
struct Trie
{
int ch[], val;
Trie()
{
Mem(ch, ); val = ;
}
}t[maxm * ];
int getnum(char c)
{
return c >= 'a' ? c - 'a' : c - 'A';
}
void insert(int id, char *s)
{
int len = strlen(s);
for(int i = , now = ; i < len; ++i)
{
int c = getnum(s[i]);
if(!t[now].ch[c]) t[now].ch[c] = ++cnt;
now = t[now].ch[c];
if(i == len - ) t[now].val = id;
}
} int ans[maxn], pre[maxn];
void query(int x)
{
for(int i = x, now = ; i >= ; --i)
{
int c = getnum(s[i]);
if(!t[now].ch[c]) return;
now = t[now].ch[c];
if(t[now].val && (ans[i - ] || i == )) {pre[x] = i - , ans[x] = t[now].val; return;}
}
return;
} void print(int x)
{
if(pre[x] != -) print(pre[x]);
printf("%s ", ss[ans[x]]);
} int main()
{
n = read(); scanf("%s", s);
m = read();
for(int i = ; i <= m; ++i)
{
scanf("%s", ss[i]);
insert(i, ss[i]);
}
for(int i = ; i < n; ++i) query(i);
print(n - ); enter;
return ;
}
CF633C Spy Syndrome 2的更多相关文章
- CF633C Spy Syndrome 2 trie树
这个模型以前绝对见过,模拟赛的时候开始敲了一个AC自动机,纯属脑抽~ code: #include <bits/stdc++.h> #define N 5000006 #define NN ...
- Manthan, Codefest 16 -C. Spy Syndrome 2
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 633C. Spy Syndrome 2 hash
题目链接 C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- 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 ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- CF#633C Spy Syndrome 2 DP+二分+hash
Spy Syndrome 2 题意 现在对某个英文句子,进行加密: 把所有的字母变成小写字母 把所有的单词反过来 去掉单词之间的空格 比如:Kira is childish and he hates ...
- Codeforce 633C. Spy Syndrome 2
C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CF633C:Spy Syndrome 2——题解
https://vjudge.net/problem/CodeForces-633C http://codeforces.com/problemset/problem/633/C 点击这里看巨佬题解 ...
随机推荐
- 一套完整的VI包含哪些元素
VI设计,即视觉识别系统,企业VI设计是企业品牌建设的重中之重.最近很多人都在问,一套完整的企业VI设计都包括哪些内容?笔者站在一个高级设计师的角度,来简单谈一谈VI设计包括哪些内容.文中指出,一套完 ...
- ffmpeg+cuda+opencv
为了让ffmpeg使用gpu进行解码,可以按以下步骤: 1 下载nvidia官网提供的ffmpeg,编译安装 https://developer.nvidia.com/ffmpeg 注意原来的选项上还 ...
- (转)CentOS/Linux 解决 SSH 连接慢
CentOS/Linux 解决 SSH 连接慢 原文:http://blog.csdn.net/doiido/article/details/43793391 现在连接linux服务器一般都是使用SS ...
- Java入门系列-09-循环结构
这篇文章为你搞懂5个问题 while 循环如何使用 do-while 循环的使用 for 循环的使用 break.continue 的使用 循环结构的嵌套使用 生活中有很多事情需要我们重复的去做,比如 ...
- 图像文字识别(OCR)用什么算法小结
说明:主要考虑深度学习的方法,传统的方法不在考虑范围之内. 1.文字识别步骤 1.1detection:找到有文字的区域(proposal). 1.2classification:识别区域中的文字. ...
- textarea的实现
由于限制字数是用原有的 maxlength会有问题,所以用一般会用js控制,今天用到三种: (一)html: <body> <form name=myform action=&quo ...
- 调用WCF错误-There was no endpoint listening
问题描述: 今天在调用WCF服务时候出现了下面的错误. 原因: 调用服务的客户端ip设置成了固定ip.(至于固定ip为什么会导致这个错误,没能去研究) 解决方法: 将客户端ip设置成自动获取.
- 科学计算基础包——Numpy
一.NumPy简介 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. 1.NumPy的主要功能 (1)ndarray:一个多维数组结构,高效且节省空间. (2)无需 ...
- 使用canvas及js简单生成验证码方法
在很多时候都需要用到验证码,前端验证码需要知道Html5中的canvas知识点.验证码生成步骤是:1.生成一张画布canvas 2.生成随机数验证码 3.在画布中生成干扰线 4.把验证码文本填充到 ...
- Elipse plugin or Bundle & OSGI
Develop and register service, lookup and use service! Android Design on service's publish-find-bind ...