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 ...
随机推荐
- python 中的input()和raw_input()功能与使用区别
在python中raw_input()和input()都是提示并获取用户输入的函数,然后将用户的输入数据存入变量中.但二者在处理返回数据类型上有差别. input()函数是raw_intput()和e ...
- Exchange Server 2007的即将生命周期,您的计划是?
多数人做微软技术,Exchange Server 几乎所有 Microsoft 产品,都具有产品支持生命周期,原厂提供的新增功能. 错误修复.安全修补程序等.产品生命周期通常持续时间 10 年,超过期 ...
- Asp.NetCore1.1版本没了project.json,这样来生成跨平台包
本章将要和大家分享的是Asp.NetCore1.1版本去掉了project.json后如何打包生成跨平台包, 为了更好跟进AspNetCore的发展,把之前用来做netcore开发的vs2015卸载后 ...
- SQL Tuning 基础概述10 - 体会索引的常见执行计划
在<SQL Tuning 基础概述05 - Oracle 索引类型及介绍>的1.5小节,提到了几种"索引的常见执行计划": INDEX FULL SCAN:索引的全扫描 ...
- 初见 ThreadLocal 类
这个类能够将一个对象和一个线程绑定起来. 之所以写这个类是因为 DBUtils 工具类,在 JavaEE 经典三层结构中对于事务的操作,不方便放在 DAO 层,因为具有侵入性,只适合放在 Servic ...
- x战警 天启高清完整版下载
天启出生于埃及第一王朝,是地球上最古老.最强大的变种人沙巴泊,曾拥有无数信众,但后来遭人背叛,被人活埋.几千年后,强大无匹而且永生不朽的天启从数千年的深埋中获释,愤怒的他发现同类不再被视为神祇,他对人 ...
- canvas 3D雪花效果
<!DOCTYPE html> <html style="height: 100%;"> <head> <meta charset=&qu ...
- 使用Python对Access读写操作
学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件. 1.导入模块 import win ...
- 移动端ios 输入框fixed固定在底部 焦点时乱跳加遮盖问题的解决 转自zhangyunling 加个人项目解决方案
如果您有过移动端的开发经验,那么您是否碰到过这样的问题,一个页面有输入框,当这个输入框聚焦时,输入框在IOS下,被移动到手机屏幕的当中去了,而在Android端,有些浏览器的输入框,会被键盘盖住. 1 ...
- 01.Nodejs入门之Helloworld
说明:本文章可供有一定js基础的朋友参考nodejs入门,本文未讲解nodejs的安装,如有需要的同学可以加QQ3382260752找我,进行交流学习. 1.新建文件夹helloworld demo, ...