CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie
题目链接:http://codeforces.com/problemset/problem/633/C
大意就是给个字典和一个字符串,求一个用字典中的单词恰好构成字符串的匹配。
比赛的时候是用AC自动机写的,就是对于trie中每一个节点,判断是否为终结点,以及当前字符所在位置p减去trie中这个节点的深度也即某一单词的长度l,判断dp[p-l]是否可以被构成,可以的话直接break并且标记当前dp值。
赛后想了想,其实直接一个trie就行了,每个单词才1000的长度,又想起来我去年给人讲过类似的问题,当时我自己非常清楚这种单词构成用个简单的tire来搞就行。怎么比赛的时候就写了个AC自动机了呢。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <numeric>
#include <cassert> using namespace std;
const int N=;
string dic[N];
char str[N],pat[N];
int mark[N];
const int CHARSET=,BASE='a',MAX_NODE=;
struct Trie {
int tot,root,child[MAX_NODE][CHARSET];
int flag[MAX_NODE];
Trie(){
init();
}
void init(){
root=newNode();
}
int newNode() {
++tot;
memset(child[tot],,sizeof(child[tot]));
flag[tot]=;
return tot;
}
void insert(const char *str,int id){
int *cur=&root;
for (const char *p=str;*p;++p){
cur=&child[*cur][*p-BASE];
if (*cur==)
*cur=newNode();
}
flag[*cur]=id;
}
void query(int x){
int *cur=&root;
for (int i=x;i>=;i--){
char ch=str[i];
cur=&child[*cur][ch-BASE];
if ((*cur)==) break;
if (flag[*cur]&&mark[i-]!=-){
mark[x]=flag[*cur];
break;
}
}
}
}trie;
int main () {
int n;
scanf("%d",&n);
scanf("%s",str+);
int m;
scanf("%d",&m);
for (int i=;i<=m;i++) {
scanf("%s",pat);
dic[i]=string(pat);
int len=dic[i].length();
for (int j=;j<=len;j++)
if (pat[j]>='A'&&pat[j]<='Z')
pat[j]+='a'-'A';
trie.insert(pat,i);
}
memset(mark,-,sizeof mark);
mark[]=;
for (int i=;i<=n;i++){
trie.query(i);
}
vector<int> ret;
int now=n;
while (now>) {
ret.push_back(mark[now]);
now-=dic[mark[now]].length();
}
for (int i=(int)ret.size()-;i>=;i--) {
printf("%s ",dic[ret[i]].c_str());
}
return ;
}
CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie的更多相关文章
- Manthan, Codefest 16 -C. Spy Syndrome 2
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- CF Manthan, Codefest 16 B. A Trivial Problem
数学技巧真有趣,看出规律就很简单了 wa 题意:给出数k 输出所有阶乘尾数有k个0的数 这题来来回回看了两三遍, 想的方法总觉得会T 后来想想 阶乘 emmm 1*2*3*4*5*6*7*8*9 ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- Manthan, Codefest 16
暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...
- Manthan, Codefest 16 D. Fibonacci-ish
D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...
- Manthan, Codefest 16(B--A Trivial Problem)
B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Manthan, Codefest 16 -A Ebony and Ivory
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- Prince2学习有感:PRINCE2项目管理到底是什么?
2007年1月份,我加入了荷兰Irdeto(中国)有限公司.刚进入公司,我就结识了Prince2(受控环境下的项目管理),才知道Prince2是英国政府在政府项目中使用的项目管理标准. 这个标准早 ...
- 读书笔记 effective c++ Item 32 确保public继承建立“is-a”模型
1. 何为public继承的”is-a”关系 在C++面向对象准则中最重要的准则是:public继承意味着“is-a”.记住这个准则. 如果你实现一个类D(derived)public继承自类B(ba ...
- PHP测试题讲解(20161027)
注: 关联数组 关联数组,它的每个 ID 键都关联一个值.在存储有关具体命名的值的数据时,使用数值数组不是最好的做法.通过关联数组,我们可以把值作为键,并向它们赋值. 例子 1 在本例中,我们使用一个 ...
- oracle 实例启动报错(ORA-01078: failure in processing system parameters )
在启动Oracle数据库时报错,如下: [oracle@localhost ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Productio ...
- Mobiscroll日期插件使用
相关资源 官方帮助文档:https://docs.mobiscroll.com/3-0-1 导入资源文件 <link href="${webRoot}/template/green/w ...
- linux vi hjkl由来
很远原因来自历史 I was reading about vim the other day and found out why it used hjkl keys as arrow keys. Wh ...
- node.js平台下Express的session与cookie模块包的配置
首先下载两个模块包 session模块包:用于保持登录状态或保持会话状态等. npm install express-session --save-dev cookie模块包:用于解析cookie. ...
- 在React中使用CSS Modules设置样式
最近,一直在看React...那真的是一个一直在学的过程啊,从配置环境webpack,到基础知识jsx,babel,es6,没有一个不是之前没有接触的.其实,我内心是兴奋的啊,毕竟,活着就是要接触一些 ...
- Oeacle创建表空间
/*第1步:创建临时表空间 */ create temporary tablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\user_tem ...
- 小练习,判断X的奇偶性
package lianxi1; public class text { public static void main(String[] args) { ; ==) { System.out.pri ...