HDU 5687 字典树入门
Problem C
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1423 Accepted Submission(s): 426
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
insert hello
insert hehe
search h
delete he
search hello
No
#include<bits/stdc++.h>
using namespace std;
struct node
{
int have;
node *child[];
node(){have=;for(int i=;i<;++i) child[i]=NULL;}
};
node *root;
void release(node *p)
{
if(p==NULL) return;
for(int i=;i<;++i){
if(p->child[i]!=NULL) release(p->child[i]);
}
delete p;
}
void Insert(char *s)
{
node *p=root;
int n1=strlen(s);
for(int i=;i<n1;++i){int t=s[i]-'a';
if(p->child[t]==NULL)
p->child[t]=new node();
p=p->child[t];
p->have++;
}
} void Delete(char *s)
{ node *p=root,*pre=p;
int n1=strlen(s),t,num=;
for(int i=;i<n1;++i){ t=s[i]-'a';
if(p->child[t]==NULL) return;
pre=p;
p=p->child[t];
}num=p->have;
release(p);
pre->child[t]=NULL;
p=root;
for(int i=;i<n1-;++i){
p=p->child[s[i]-'a'];
p->have-=num;
}
}
bool Search(char *s)
{
node *p=root;
int n1=strlen(s);
for(int i=;i<n1;++i){
int t=s[i]-'a';
if(p->child[t]==NULL) return ;
p=p->child[t];
}
if((p->have)<) return ;
return ;
}
int main()
{
int N,i,j;
char s1[],s2[];
cin>>N;
root=new node();
while(N--){
scanf("%s%s",s1,s2);
if(!strcmp(s1,"insert")){
Insert(s2) ;
}
else if(!strcmp(s1,"delete")){
Delete(s2);
}
else if(!strcmp(s1,"search")){
Search(s2)?puts("Yes"):puts("No");
}
}release(root);
return ;
}
HDU 5687 字典树入门的更多相关文章
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- hdu 1247 (字典树入门)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- HDU 1251 统计难题(字典树入门模板题 很重要)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- 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 ...
随机推荐
- 控制台应用程序中添加对MFC的支持
在windows控制台应用程序中,肯能会想使用一些MFC中的类,如CStringArray等,通过下面两步简单的设置可以添加对MFC的支持: 1.右击工程名 -> References 选择 A ...
- (转)extern关键字两种场景的使用
第一种场景 -- extern extern关键字的作用是声明变量和函数为外部链接,即该变量或函数名在其它文件中可见.用其声明的变量或函数应该在别的文件或同一文件的其它地方定义. 例如语句:exter ...
- JUnit之参数化测试、套件/成组测试的使用
原文地址http://blog.csdn.net/yqj2065/article/details/39967065 参数化测试 正如数组替代int a0,a1,a2一样,测试加法时assertEqua ...
- XDU 1056
解法一:简单搜索肯定TLE,只是单纯的想写一发搜索练练手 #include<cstdio> #include<cstring> #define maxn 1005 using ...
- PL/SQL编程—存储过程
SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...
- Mybatis 一对一、一对多、多对多
一对一返回resultType <!-- 查询订单关联查询用户信息 resultType --> <select id="findOrderCustom" res ...
- cocos2dx 3.x HttpRequest GET获取数据
.h文件 #include "network/HttpClient.h" //包含头文件 // GET 函数 void createGetHttp(); void getHttp_ ...
- 解决日志unicode编码问题
Xcode打印日志里如果有汉字,有时会以Unicode编码形式展示,另需工具转码,降低了开发的效率,未解决这一问题,简便的解决方法是 pod 'HYBUnicodeReadable' 这个第三方库很好 ...
- Java线程常用方法汇总
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
- redis日常操作
redis针对所有类型的日常操作: keys * ## 取出所有key keys my* ## 模糊匹配 exists name ## 存在name键返回1,否则返回0 del key1 ## 删除一 ...