#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. document的一点点

    var str = "hello world" document.write(str.length)输出字符串长度   12 给string 添加样式 <p>Big: ...

  2. js中获得指定范围的n个不重复的随机数

    function getarray(arr,num){ //复制数组,不能直接在原数组上修改 var temparr=new Array(); //遍历原数组 for(var i in arr){ / ...

  3. 【转载】AngularJs 指令directive之controller,link,compile

    关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...

  4. 【Jenkins】jenkins简单搭建并执行任务

    part 1  jenkins用户名的配置(gerrit 2.11) #安装jenkins $ sudo apt-get install jenkins #为jenkins用户设定密码 $ sudo ...

  5. Python图表绘制:matplotlib绘图库入门

    matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  6. linux -小记(2)问题:yum 安装报错"Another app is currently holding the yum lock; waiting for it to exit... ...: yum Memory : 26 M RSS (868 MB VSZ) Started: Wed Oct 26 22:48:24 2016 - 0"

    yum 安装报错 "Another app is currently holding the yum lock; waiting for it to exit... The other ap ...

  7. STM32F1和STM32F4 区别

    STM32F4相对于STM32F1的改进不只一点点,为了便于初学者了解,我们比对相关资料将改进点进行了汇总. STM32F1和STM32F4 区别   (安富莱整理)u  F1采用Crotex M3内 ...

  8. ThinkPad E440 Ubuntu 13.1无线网卡 RTL8723BE 驱动解决办法总结

    方法一.在使用网线链接到情况下 第一步.执行下边到命令,即从github上下载驱动程序并安装 sudo apt-get install linux-headers-generic build-esse ...

  9. [家里蹲大学数学杂志]第235期$L^p$ 调和函数恒为零

    设 $u$ 是 $\bbR^n$ 上的调和函数, 且 $$\bex \sen{u}_{L^p}=\sex{\int_{\bbR^n}|u(y)|^p\rd y}^{1/p}<\infty. \e ...

  10. python的中文编码问题

    下文转自 http://blog.csdn.net/mayflowers/article/details/1568852 1.        在Python中使用中文 在Python中有两种默认的字符 ...