嘟嘟嘟

题面:把一句话加密: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的更多相关文章

  1. CF633C Spy Syndrome 2 trie树

    这个模型以前绝对见过,模拟赛的时候开始敲了一个AC自动机,纯属脑抽~ code: #include <bits/stdc++.h> #define N 5000006 #define NN ...

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

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

  3. codeforces 633C. Spy Syndrome 2 hash

    题目链接 C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

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

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

  5. 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 ...

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

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

  7. CF#633C Spy Syndrome 2 DP+二分+hash

    Spy Syndrome 2 题意 现在对某个英文句子,进行加密: 把所有的字母变成小写字母 把所有的单词反过来 去掉单词之间的空格 比如:Kira is childish and he hates ...

  8. Codeforce 633C. Spy Syndrome 2

    C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. CF633C:Spy Syndrome 2——题解

    https://vjudge.net/problem/CodeForces-633C http://codeforces.com/problemset/problem/633/C 点击这里看巨佬题解 ...

随机推荐

  1. fastclick.js源码解读分析

    阅读优秀的js插件和库源码,可以加深我们对web开发的理解和提高js能力,本人能力有限,只能粗略读懂一些小型插件,这里带来对fastclick源码的解读,望各位大神不吝指教~! fastclick诞生 ...

  2. (转)Linux网络状态工具ss命令使用详解

    Linux网络状态工具ss命令使用详解 原文:http://www.landui.com/help/show-5991.html ss 是 socket statistics 的缩写.顾名思义,ss ...

  3. docker~run起来之后执行多条命令

    最近在搞jenkins pipeline的部署工作,而在对.net core进行部署时不希望安装dotnet sdk,为了移植性更好,打算直接使用aspnetcore的docker镜像,通过docke ...

  4. poj 1028 Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31088   Accepted: 13933 ...

  5. [Matlab] fprintf

    %s format as a string%d format with no fractional part (integer format)%f format as a oating-point v ...

  6. HDU 1257——最少拦截系统——————【LIS变型题】

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  7. bzoj 5340: [Ctsc2018]假面

    Description 题面 Solution 生命值范围比较小,首先维护每一个人在每个血量的概率,从而算出生存的概率,设为 \(a[i]\) 询问时,只需要考虑生存的人数,可以 \(DP\) 设 \ ...

  8. eclipse-java-style.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><profil ...

  9. 在 UWP 应用中创建、使用、调试 App Service (应用服务)

    在 Windows 10 中微软为 UWP 引入了 App Service (即应用服务)这一新特性用以提供应用间交互功能.提供 App Service 的应用能够接收来自其它应用传入的参数进行处理后 ...

  10. 弹性布局学习-详解 align-items(四)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...