#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct Tree
{
Tree *next[];
bool isVirus;
int num;
};
Tree *root;
int all;
char temp[];
int temps[];
int n,m;
void insert(char temp[],int Num)
{
int len=strlen(temp);
Tree *the=root;
for(int i=; i<len; i++)
{
int temps=temp[i]-' ';
if(the->next[temps]==NULL)
{
Tree* ttemp=(Tree *)malloc(sizeof(Tree));
for(int j=; j<; j++)
ttemp->next[j]=NULL;
ttemp->isVirus=false;
if(i==len-)
{
ttemp->isVirus=true;
ttemp->num=Num;
}
the->next[temps]=ttemp;
}
else if(i==len-)
{
the->next[temps]->isVirus=true;
the->next[temps]->num=Num;
}
the=the->next[temps];
}
}
void search(int num)
{
int virusNum=;
int vir[];
int len=strlen(temp);
for(int i=; i<len; i++)
{
Tree* the=root;
for(int j=; i+j<len; j++)
{
int ttemp=(int)(temp[i+j]-' ');
if(the->next[ttemp]==NULL)break;
else if(the->next[ttemp]->isVirus)
{
bool ok=false;
for(int s=; s<virusNum; s++)
{
if(vir[s]==the->next[ttemp]->num)
ok=true;
}
if(!ok)
{
vir[virusNum++]=the->next[ttemp]->num;
i=i+j;
break;
}
else
the=the->next[ttemp];
}
else
the=the->next[ttemp];
}
if(virusNum==)break;
}
if(virusNum)
{
all++;
printf("web %d:",num);
sort(vir,vir+virusNum);
for(int i=; i<virusNum; i++)
printf(" %d",vir[i]);
printf("\n");
}
}
int main()
{
int virusNum;
int vir[];
root=(Tree *)malloc(sizeof(Tree));
for(int i=; i<; i++)
root->next[i]=NULL;
root->isVirus=false;
cin>>n;
for(int i=; i<=n; i++)
{
scanf("%s",temp);
insert(temp,i);
}
cin>>m;
for(int i=; i<=m; i++)
{
scanf("%s",temp);
search(i);
}
printf("total: %d\n",all);
return ;
}

本题很水,直接字典树可以a掉,不过听说是ac自动机模板题,有学ac自动机的想法,后面也许会贴上ac自动机的代码。

hdu 2896 字典树解法的更多相关文章

  1. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  2. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  3. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  5. Chip Factory HDU - 5536 字典树(删除节点|增加节点)

    题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...

  6. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  7. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  8. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

  9. Hat’s Words HDU - 1247 字典树

    题意:给出数个单词 输出单词 如果该单词 是由字典中的单词组成的 思路:字典树 先把全部建树  然后对于每一个单词进行分割,分别拿两半到字典树里面去找 小心RE! #include<bits/s ...

随机推荐

  1. linux下压缩与解压(zip、unzip、tar)详解

    linux下压缩与解压(zip.unzip.tar)详解 2012-05-09 13:58:39| 分类: linux | 标签:linux zip unzip tar linux命令详解 |举报|字 ...

  2. python 的内嵌time模板翻译及说明[转]

    一.简介 time模块提供各种操作时间的函数  说明:一般有两种表示时间的方式:       第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的      ...

  3. android图像与图像处理系列(一、Bitmap和BitmapFactory)

    1.Drawable对象 Android应用添加了Drawabe资源之后,Android SDK会为这份资源文件在R清单文件中创建一个索引项:R.drawable.file_name,接着我们可以在x ...

  4. eclipse导入github项目

    以jeesite为例, github上面的项目大都是基于git方式进行版本控制以及使用maven构建的项目. 1 使用时,需先用eclipse的以git方式从github上下载代码. 下载后得到mav ...

  5. [转]Windows系统中监控文件复制操作的几种方式

    1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...

  6. 微分方程——包络和奇解

    对某些微分方程,存在一条(也可能多条)特殊的积分曲线,它并不属于方程的积分曲线族.但是,在这条特殊的积分曲线上的每一点处,都有积分曲线族中的一条曲线和它在此点相切.在几何学上,这条特殊的积分曲线称为上 ...

  7. 用友的凭证update

    select pk_glorgbook from bd_glorgbook where glorgbookcode='0100-0001';--0001N510000000006K4X ' and p ...

  8. 升级NC6.3

    2014-04-23 江苏建工&用友公司会谈提纲 1,合同规定江苏建工用友NC在实施成功之后三年免服务费(2010年增补了资金管理,如果以2010年作为软件最终实施完成,那么2010-2013 ...

  9. PHP中float变量转换为int时,结果有误的问题!

    先上例子: <?php $money = 100; $rate = 1.15; $result = $money * $rate; var_dump( intval( $result ) ); ...

  10. UML精粹1 - 简介

    Martin的主页 http://martinfowler.com/. Pavel Hruby开发的visio模板,可以用来画UML图: http://phruby.com 简介 统一建模语言UML是 ...