hdu-5687(字典树)
题意:中文题;
解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个次数,最后节点的儿子全部归零;
最开始我是只考虑的最后节点,中间节点都没考虑,这样会出现一个问题就是:插入abdefg,删除abcd的时候,查询abc还是会有答案,所有中间过程也得减去;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1200500
using namespace std;
int trie[maxn][];
int num[maxn]={-};
int root;
int tot;
int t,n;
void init()
{
memset(trie,,sizeof(trie));
memset(num,,sizeof(num));
tot=;
}
void get_trie(char *s)
{
root=;
int slen=strlen(s);
for(int i=;i<slen;i++)
{
int id=s[i]-'a';
if(!trie[root][id])
{
trie[root][id]=++tot;
}
root=trie[root][id];
num[root]++;
}
}
int query(char *s)
{
root=;
int slen=strlen(s);
for(int i=;i<slen;i++)
{
int id=s[i]-'a';
if(!trie[root][id])
{
return;
}
root=trie[root][id];
}
return num[root];
}
void delete_trie(char *s)
{
root=;
int cnt=;
int slen=strlen(s);
for(int i=;i<slen;i++)
{
int id=s[i]-'a';
if(!trie[root][id])
{
return;
}
root=trie[root][id];
}
cnt=num[root];
root=;
for(int i=;i<slen;i++)
{
int id=s[i]-'a';
root=trie[root][id];
num[root]=num[root]-cnt;
}
for(int i=;i<=;i++)
trie[root][i]=;
}
int main()
{
char s[],a[];
scanf("%d",&t);
init();
while(t--)
{
scanf("%s",s);
scanf("%s",a);
if(s[]=='i')
{
get_trie(a);
}
else if(s[]=='s')
{
int flag=query(a);
if(flag==)
printf("No\n");
else
printf("Yes\n");
}
else
{
delete_trie(a);
}
}
}
hdu-5687(字典树)的更多相关文章
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- hdu 1251 字典树的应用
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...
- hdu 2896 字典树解法
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...
随机推荐
- NLog配置分享
新建一个文件命名为NLog.Config,然后添加如下代码 <?xml version="1.0" encoding="utf-8" ?> < ...
- 如何添加一种新Case协议
这里以添加基础http为例 首先要在脚本文件(XML文件)中定义好这种协议的基本信息 您必须在这里设计好您协议预先需要的数据(比如串口协议,那波特率,串口号等可能是不会经常改变的就可以在这里先 ...
- NPOI生成excel并下载
NPOI文件下载地址:http://npoi.codeplex.com/ 将文件直接引用至项目中即可,,,,, 虽然网上资料很多,但有可能并找不到自己想要的功能,今天闲的没事,所以就稍微整理了一个简单 ...
- Python-递归初识-50
#递归函数 # 了解什么是递归 : 在函数中调用自身函数 # 最大递归深度默认是997/998 —— 是python从内存角度出发做得限制 # 能看懂递归 # 能知道递归的应用场景 # 初识递归 —— ...
- Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)
D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...
- sql 查询优化小计
好久没更博了,偷偷的抽时间写一下. 早上开始working的时候,发现一个页面加载很慢,经排查是昨天写的一条联合查询的sql导致的.于是着手优化! 首先想到的是在join的时候,减少表体积之后再进行关 ...
- PHP开发编码规范
(转载:https://blog.csdn.net/alexdream/article/details/2213313) 这些年来多从事Linux下PHP和C相关的开发,带过很多项目和团队,下面是根据 ...
- 百度地图api在Html中显示,在jsp页面中不显示解决方法
在jsp页面中显示如下 但是在html中正常显示. 原来的代码如下: <script type="text/javascript" src="http://api. ...
- vsconsole
一.安装 npm install vconsole 二. if (process.env.NODE_ENV === `development`) { const { logger } = requir ...
- JavaScript生成二维码图片
1.引入一个二维码工具的js文件,同时需要引入jquery文件 下面是jquery.qrcode.min.js文件内容: (function(r){r.fn.qrcode=function(h){va ...