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 ...
随机推荐
- TP框架数据库操作(增删改)
首先选择一张表,对其进行操作: 对数据库操作之前首先要创建模型: $n = M("account"); 数据库添加数据: 1.使用数组: 1.使用数组 $arr = array(& ...
- ionic的安装
一.学习一样新的框架的步骤: 1.先找到人家的网站, 一个个点过来看看 2.我们前端的框架,分css与js 3.先学css 再学js 4.要学会复制黏贴代码, 实际演练代码的效果 二.ionic环境安 ...
- Spring 容器可以在自动装配相互协作的 bean 之间的关系,使用autowire属性定义指定自动装配模式。
使用setter方法java public class TextEditor { private SpellChecker spellChecker; public void setSpellChec ...
- UML软件方法大纲
利用周末的时间读了潘加宇的<软件方法(上)>,希望梳理清楚UML的知识脉络: 工作流 子流程 内容 备注 建模和uml 利润=需求-设计 愿景 缺乏清晰.共享的愿景往往是项目失 ...
- django进阶-3
先看效果图: 登陆admin后的界面: 查看作者: 当然你也可以定制admin, 使界面更牛逼 数据库表结构: app01/models.py from django.db import models ...
- Java完成简单猜数字游戏v2.0
猜数字游戏v2.0 优化了获取随机数.输入数据超出边界值的代码,并增加了异常处理,能够在玩家输入错误数据错误时给出可靠指引,希望对和我一样的新人有帮助, 最后希望有大神愿意帮我解决代码优化的问题,谢谢 ...
- 让我的分页类获取sessionFactory
我们知道在Hibernate里比较重要的sessionFactory,经过Spring的管理可以很好地为Spring里注入使用的bean服务(提供数据源的使用),但是,当我们所要使用的类不是像我们尝试 ...
- LVS的原理介绍
DR模式 LVS 的VIP 和 realserver 必须在同一个网段,不然广播后所有的包都会丢掉: 提前确认LVS/硬件LB 是什么模式,是否需要在同一个网段 所有的realserver 都必须绑 ...
- XJOI1680阿猫的实验
阿猫的实验 阿猫很喜欢生物学.他还在今年的全国中学生生物学联赛中获得了一等奖.一天,阿猫在实验室听说了这样一种繁殖能力很强的老鼠.这种老鼠在出生后的第一个月,可以生出a 对老鼠:第二个月,可以生出b ...
- ng指令控制一个元素的影藏的与显示几种方法的使用
在ng中我们控制一个元素的显示与隐藏的方法: (1):ng-show=true/false 解释:ng-show使用的是display="block"/"none&quo ...