题意:中文题;

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

最开始我是只考虑的最后节点,中间节点都没考虑,这样会出现一个问题就是:插入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. Feature Extractor[inception v2 v3]

    0 - 背景 在经过了inception v1的基础上,google的人员还是觉得有维度约间的空间,在<Rethinking the Inception Architecture for Com ...

  2. DAG也许是真正的区块链3.0

    从15年开始,区块链概念被单拎出来,这之前区块链还只是比特币技术里的一个数据结构,中本村白皮书里把block和chain连一起的时候也只是a chain of blocks .随着以太坊去中心化计算机 ...

  3. java 基础04 重写

  4. Gradle构建工具从入门到精通(IDEA)

    1.Gradle安装 官网下载压缩包,然后解压,配置本地环境变量.主要有下面两个: GRADLE_HOME 是解压后的目录, GRADLE_USER_HOME 的作用是让其他程序检测到本地.gradl ...

  5. 能递归检查DataAnnotations的验证函数

    有时在Command和DTO之间层次比较多,写了个验证Command的函数,能实现递归验证. 比如下面这些有层级关系的class定义,能通过一句代码来进行验证: class A { [Required ...

  6. 基于linux下的krpano的使用

    鉴于目前网络上关于krpano的使用和介绍少之又少,结合自己的学习和使用经历,做个总结和记录. 1.安装 下载地址: linux https://krpano.com/forum/wbb/index. ...

  7. 配置router列表

    import Vue from "vue"; import VueRouter from 'vue-router'; import Star from '../components ...

  8. Oracle 创建外部表

    Oracle 外部表能迅速的将海量的数据导入到数据库里面,外部表的创建使用步骤如下: 1 创建一个Directory:必须用sys用户创建,用户存放外部数据文件. create directory D ...

  9. mybatis出现NoSuchMethodException异常

    今天在idea中调试项目(ssm搭建的项目)的时候,mybatis突然出现了NoSuchMethodException异常,具体的异常时: java.lang.NoSuchMethodExceptio ...

  10. [转帖]PAT 计算机程序设计能力考试

    PAT 计算机程序设计能力考试 https://blog.csdn.net/bat67/article/details/52134211 [官方简介] 计算机程序设计能力考试(Programming ...