HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687
2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解
#include<stdio.h>
#include<string.h>
struct node{
int next[];
int cnt;
void init(){
cnt = ;//计数
memset(next,-,sizeof(next));
}
};
struct node trie[];
int tot = ;//记录节点数 void insert(char *s){
int cur = ,len = strlen(s);
for(int i = ;i<len;i++){
int temp = s[i]-'a';
int next = trie[cur].next[temp];
if(next == -){
next = ++tot;
trie[next].init();
trie[cur].next[temp] = next;
}
cur = next;
trie[cur].cnt++;
}
}
int find(char *s){
int cur = ,len = strlen(s);
for(int i = ;i<len;i++){
int temp = s[i]-'a';
int next = trie[cur].next[temp];
if(next == -)
return ;
cur = next;
}
return trie[cur].cnt>;//注意
}
void del(char *s){
int cur = ,late,len = strlen(s);
//这里特判一下要删除的字符串是否存在
for(int i=;i<len;i++){
int tmp=s[i]-'a';
int next=trie[cur].next[tmp];
if(next==-) return;
late = cur;
cur=next;
}
cur = ;
for(int i = ;i<len;i++){
int temp = s[i]-'a';
int next = trie[cur].next[temp];
late = cur;
cur = next;
trie[cur].cnt--;
}
trie[cur].init();
int temp = s[len-]-'a';
trie[late].next[temp] = -;
}
int main(){
int n;
scanf("%d",&n);
char op[],s[];
trie[].init();
while(n--){
scanf(" %s %s",op,s);
if(op[] == 'i'){
insert(s);
}else if(op[] == 's'){
if(find(s))
puts("Yes");
else
puts("No");
}else{
del(s);
}
}
return ;
}
HDU 5687 字典树插入查找删除的更多相关文章
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- B树——插入和删除
B树--插入和删除 B树的插入 5阶B数--结点关键字个数向上取整m/2-1≤n≤m-1 即2≤n≤4 连续插入5个元素后,超出来了. 在插入key后,若导致原结点关键字数超过上限,则从中间位置(m/ ...
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- AVL树插入和删除
一.AVL树简介 AVL树是一种平衡的二叉查找树. 平衡二叉树(AVL 树)是一棵空树,或者是具有下列性质的二叉排序树: 1它的左子树和右子树都是平衡二叉树, 2且左子树和右子树高度之差的 ...
- hdu 1247 (字典树入门)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 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 1075(字典树)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
随机推荐
- CSS--字体
通用字体系列 CSS中定义了5种通用字体系列 举例说明:指定通用字体系列 body { font-family:sans-serif;/*如果你希望文档使用一种sans-serif字体而并不关心是哪一 ...
- UML 简单介绍
Unified modeling Language - 统一建模语言
- web前端性能优化
性能优化对于用户体验无疑是非常重要的,下面介绍一些性能优化的方法. 1.减少HTTP请求 http请求越多,那么消耗的时间越多,如果在加上网络很糟糕,那么问题就更多了.且如果网页中的图片.css文件. ...
- uC/OS-II信号(OS_sem)块
/*************************************************************************************************** ...
- 提取hdfs文件名的方法
#!/bin/bash #------------------------------------------------------------- # 把目录中的所有文件getmerge到本地 # ...
- 安装beautifulsoup4
python scripts下 pip install beautifulsoup4
- ubuntu分区
参考网址:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html
- SSAO相关资料
http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-pract ...
- sky
UniSky入门资料 http://game.ceeger.com/forum/read.php?tid=4069
- 网络框架 & 云端
Scut 傲瑞组件开发手册(02) -- 轻量级的通信引擎 StriveEngine http://blog.oraycn.com/StriveEngine.aspx