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 点击这里看巨佬题解 ...
随机推荐
- python从字符串内取两个符号之间的内容
#取字符串中两个符号之间的东东 def txt_wrap_by(self,start_str, end, html): start = html.find(start_str) if start &g ...
- 常用shell脚本(持续更新中)
1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...
- 深入理解JavaScript系列(33):设计模式之策略模式
介绍 策略模式定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户. 正文 在理解策略模式之前,我们先来一个例子,一般情况下,如果我们要做数据合法性验证,很 ...
- 深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
本篇是ECMA-262-3 in detail系列的一个概述(本人后续会翻译整理这些文章到本系列(第11-19章).每个章节都有一个更详细的内容链接,你可以继续读一下每个章节对应的详细内容链接进行更深 ...
- 5、Angular2 Injectable 服务
1.Injectable
- Sql批处理语句
同时写3个批处理,如果前2个批处理没有问题,最后一个有错误那么3个批处理都不会执行需要注意列如: use Materl GO select * from t_icitem GO inset into ...
- Use the list and while to Build Shop car
#Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...
- Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- IO流之File类
IO概述: 程序数据都是在内存中,程序运行结束,这些数据将清空,数据都都不能保存下来,下次程序启动的时候,想再把这些数据读出来继续使用,把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化 ...
- 【数据库】4.0 MySQL入门学习(四)——linux系统环境下MySQL安装
1.0 我的操作系统是CentOS Linux release 7.6.1810 (Core) 系统详细信息如下: Linux version 3.10.0-957.1.3.el7.x86_64 ( ...