题意:中文题;

解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个次数,最后节点的儿子全部归零;

最开始我是只考虑的最后节点,中间节点都没考虑,这样会出现一个问题就是:插入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(字典树)的更多相关文章

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

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

  2. HDU 5687 字典树入门

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

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

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

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

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

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

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

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

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

  7. hdu 1251 字典树的应用

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

  8. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  9. Repository HDU - 2846 字典树

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

  10. Phone List HDU - 1671 字典树

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

随机推荐

  1. [TPYBoard - Micropython之会python就能做硬件 8] 学习使用超声波模块制作避障小车

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604   一.实验器材 1.TPYboard V102板  一块 2.电机驱动模块L2 ...

  2. Generative Adversarial Nets[Vanilla]

    引言中已经较为详细的介绍了GAN的理论基础和模型本身的原理.这里主要是研读Goodfellow的第一篇GAN论文. 0. 对抗网络 如引言中所述,对抗网络其实就是一个零和游戏中的2人最小最大游戏,主要 ...

  3. 有关Web常用字体的研究?

    Windows自带字体: 黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong 楷体:KaiTi 仿宋GB2312:FangSongGB2312 楷体GB2312:K ...

  4. Gulp压缩前端CS,JS,图片文件

    Gulp 基于Node.js的前端构建工具,Gulp有许多插件(这里是插件),使用Gulp可以实现前端代码的编译(sass.less).压缩.图片的压缩等,当然主要是前端工程化,不过我目前只是需要压缩 ...

  5. npm install 安装报错:npm ERR! EPERM npm ERR! -4048 npm ERR! Error: EPERM: operation not permitted, unlink 'D:\test\demo\code\materialT\node_modules\.staging'

    更新项目依赖包,删除掉package-lock.json.node_modules,运行npm install,报如上错误信息,查询资料说是没有权限,本人用管理员身份打开powershell,运行np ...

  6. 性能调优8:分组聚合 - group by

    聚合实际上对数据做分组统计,SQL Server使用两种操作符来实现聚合,流聚合(Stream Aggregation)和哈希聚合(Hash aggration).流聚合是非阻塞性的,具有流的特性,流 ...

  7. Python_迭代器_35

    迭代器 # l = [1,2,3]# 索引# 循环 for# for i in l:# i## for k in dic:# pass #可以被for循环的# list# dic# str# set# ...

  8. Python学习第十篇——函数初步

    def make_album(name,album_name,song_nums = 1): dict_album = {name:[album_name]} if int(song_nums) &g ...

  9. Podfile文件用法详解

    https://www.jianshu.com/p/b8b889610b7e 2018.01.09 15:51* 字数 2343 阅读 6263评论 3喜欢 34 前言 iOS开发会经常用到cocoa ...

  10. Jenkins [Error] at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:192)

    Deploying /root/.jenkins/workspace/zgg-crm-pre/target/crm.war to container Tomcat 7.x Remote with co ...