hdu 2896 字典树解法

时间:2022-01-08 18:54:01
 #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&lt ...

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

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

  3. hdu 2112&lpar;字典树&plus;最短路&rpar;

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

  4. hdu 2072&lpar;字典树模板,set,map均可做&rpar;

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

  5. Chip Factory HDU - 5536 字典树&lpar;删除节点&vert;增加节点&rpar;

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

  6. hdu 1251 字典树的应用

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

  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. C&num; 发送邮件 附件名称为空

     示例代码: // 1.创建邮件 MailMessage mailMsg = new MailMessage(); mailMsg.To.Add(new MailAddress("test@ ...

  2. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  3. ubuntu上安装Eclipse时遇到的一个错误

    A Java Runtime Environment (JRE) or Java Development Kit (JDK)must be available in order to run Ecli ...

  4. 【转载】B树、B-树、B&plus;树、B&ast;树

    转载自http://blog.csdn.net/manesking/archive/2007/02/09/1505979.aspx B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和 ...

  5. Liunx vi编辑器一些指令

    最近几天学习了Liunx vi编辑器 的使用,感觉还比较容易.总结的一点心得: vi分为3个模式,命令模式,尾行模式,编辑模式. 1. 命令模式 与 编辑模式切换 a:光标向后移动一位进入编辑模式 i ...

  6. &lbrack;译&rsqb;&period;NET Framework 4&period;8发布

    原文地址:https://devblogs.microsoft.com/dotnet/announcing-the-net-framework-4-8/ 我们很高兴地宣布今天发布.NET Framew ...

  7. Windows 上的 SSH?使用 PowerShell Remoting 远程管理 Windows 服务器

    作者:陈计节 个人博客:https://blog.jijiechen.com/post/powershell-remoting/ 在 Linux/Unix 世界里 SSH 是个好东西,SSH 是 Se ...

  8. SpringBoot各类扩展点详解

    一.前言 上篇文章我们深入分析了SpringBoot的一站式启动流程.然后我们知道SpringBoot的主要功能都是依靠它内部很多的扩展点来完成的,那毋容置疑,这些扩展点是我们应该深入了解的,那么本次 ...

  9. Error after SQL Server 2012 installation&colon; Login Failure for &quot&semi;SQL Server Integration Services 11&period;0&quot&semi; SSIS service

    When you install SQL Server 2012 and you try to connect to SSIS services, you cannot due to that the ...

  10. 前端程序员:月薪 5K 到 5 万

    入行行头:5 大硬件 请准备好以下东西 一颗人类的大脑:智商在平均水平线以上即可 一份强烈的渴望:我的代码要可以运行在任何一个有浏览器的设备上. 一台笔记本电脑:不需要花费很多钱得那种,只要它可以运行 ...