ACM: 强化训练-百度之星-Problem C-字典树
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Description
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
Input
Output
Sample Input
Sample Output
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cmath"
using namespace std;
#define MX 11111
#define memset(x,y) memset(x,y,sizeof(x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 struct node {
int v;
int next[27];
void init() {
v=0;
memset(next,-1);
}
} dir[1200500];
int tot=0; void BuildTrie(char p[]) {
int len=strlen(p);
int now=0;
for(int i=0; i<len; i++) {
int t=p[i]-'a';
if(dir[now].next[t]==-1) {
tot++;
dir[tot].init();
dir[now].next[t]=tot;
}
now=dir[now].next[t];
dir[now].v++; //记路达到该节点的次数
}
} int Query(char p[]) {
int len=strlen(p);
int now=0;
for(int i=0; i<len; i++) {
int t=p[i]-'a';
if(dir[now].next[t]==-1)return 0;
now=dir[now].next[t];
if(dir[now].v<=0) return 0;
}
return 1;
} /*/
删除函数有几个要注意的地方:
1.删除要把所有的以s为前缀的尾巴全部去掉;
2.不仅仅要把后面所有的节点都删除,还要把那些以p为前缀的长字符串的前缀也要去掉相印个数。
/*/
void Delete(char p[]) {
int now=0;
int len=strlen(p);
for(int i=0; i<len; i++) {
int t=p[i]-'a';
if(dir[now].next[t]==-1)return ;
now=dir[now].next[t];
}
int d=dir[now].v;//记录要删除的节点最后的次数,前面每一个节点都要去掉这个此时
dir[now].init();//清除尾节点后面所有的节点
now=0;
for(int i=0; i<len; i++) {
int t=p[i]-'a';
now=dir[now].next[t];
dir[now].v-=d;
if(dir[now].v<0)dir[now].v=0;//判断,不能为负数
}
} int main() {
int T;
scanf("%d",&T);
char op[10],s[40];
memset(op,0);
memset(s,0);
dir[0].init();//【这里忘记清空根节点。WA哭了。。。】
while(T--) {
scanf("%s %s",op,s);
if(op[0]=='i')BuildTrie(s);
else if(op[0]=='s') {
int ok=Query(s);
if(ok>0)printf("Yes\n");
else printf("No\n");
} else if(op[0]=='d') {
int del=Query(s);
if(del) Delete(s);
}
}
return 0;
}
ACM: 强化训练-百度之星-Problem C-字典树的更多相关文章
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- ACM: 强化训练-Roads in the North-BFS-树的直径裸题
Roads in the North Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu De ...
- ACM: 强化训练-Beautiful People-最长递增子序列变形-DP
199. Beautiful People time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard ...
- ACM: 强化训练-Inversion Sequence-线段树 or STL·vector
Inversion Sequence Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%lld & %llu D ...
- ACM: 强化训练-海贼王之伟大航路-dfs-枝减
海贼王之伟大航路 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descriptio ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- Python强化训练笔记(一)——在列表,字典,集合中筛选数据
列表,字典,集合中根据条件筛选数据,如下所示 列表:[-10,2,2,3,-2,7,6,9] 找出所有的非负数 字典:{1:90,2:55,3:87...} 找出所有值大于60的键值对 集合:{2,3 ...
- 2016集训测试赛(二十)Problem B: 字典树
题目大意 你们自己感受一下原题的画风... 我怀疑出题人当年就是语文爆零的 下面复述一下出题人的意思: 操作1: 给你一个点集, 要你在trie上找到所有这样的点, 满足点集中存在某个点所表示的字符串 ...
- HDU 5687 Problem C ( 字典树前缀增删查 )
题意 : 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字典中删除所有前缀等于给定字符串的单词 3.search: ...
随机推荐
- windows重新获取IP
win+r------->cmd------>ipconfig /release (释放ip) ipconfig /renew 重新获取ip
- [JAVA] IOException: Invalid byte 2 of 2-byte UTF-8 sequence(解决办法)
日志打印不全,后台只打印出出标题的异常信息: 先前的日志打印信息:log.debug(e.getMessage()); 后面改成了日志打印信息:log.debug(e); log.debug(e.ge ...
- ortp库入门
转自:http://blog.csdn.net/suer0101/article/details/7333267 再补充一个代码走读:http://www.xuebuyuan.com/1863409. ...
- PMP 第三章 单个项目的项目管理标准
1 项目管理五大过程组分别是什么? 启动过程组 规划过程组 执行过程组 监控过程组 收尾过程组 2 启动项目组是干什么?包含哪些过程?每个阶段都需要启动吗? 启动过程组:获得授权,定义一个新项目或现有 ...
- 解决linux下unzip中文有乱码的问题
xxx.zip 中有中文的文件,在linux下unzip就会有乱码. 解决办法:安装7zip 去http://sourceforge.net/projects/p7zip/files/latest/d ...
- 1.ok6410移植bootloader,移植u-boot,学习u-boot命令
ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...
- 工厂模式/factory模式/创建型模式
工厂模式 普通工厂模式 原本需要new出来的对象,通过一个类的方法去搞定,Factory.build(parameter),类似这种. public interface Sender { public ...
- Arduino101学习笔记(七)—— 时间API
1.毫秒时间 获取机器运行的时间长度, 单位毫秒. 系统最长的记录时间为9小时22分, 如果超出时间将从0开始. 警告: 时间为 unsigned long类型, 如果用 int 保存时间将得到错误结 ...
- XCL-Charts图表库简要教程及常见问题
这个Andriod图表库项目从开始至现在,热情消耗几近殆尽.还好已基本实现我想做的那些东西.趁还剩下点兴趣,把一些点非常简单的归纳一下. 所支持的图表类型: 基类 ...
- text()、html() 以及 val()的区别
text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的值 下面的例子演示如何通过 text().htm ...