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: ...
随机推荐
- Jquery学习笔记---闭包
1. 简要介绍 闭包可谓是js中的一大特色了,即使你对闭包没概念,你可能已经在不知不觉中使用到了闭包.闭包是什么,闭包就是一个函数可以访问到另一个函数的变量.这就是闭包,解释起来就这么一句话,不明白? ...
- php获取一维,二维数组长度的方法(有实例)
在php中获取数组长度方法很简单,php为我们提供了两个函数可以计算一维数组长度,如count,sizeof都可以直接统计数组长度哦,下面我们来看几个实例吧.php如何获取数组的长度,使用php函数c ...
- 【mysql中myisam和innodb的区别】
单击进入源网页 要点摘要: 1.查看mysql存储引擎的状态mysql> show engines; 2.查看mysql默认的存储引擎mysql> show variables like ...
- windows常用命令
打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通过cmd /c 命令 和 cmd /k 命令的方式来直接运行命令 注:/c表示执行完命令后关闭cmd ...
- [LeetCode] Happy Number
Happy Number Total Accepted: 35195 Total Submissions: 106936 Difficulty: Easy Write an algorithm to ...
- JS判断输入值是否为正整数
JS中的test是原来是JS中检测字符串中是否存在的一种模式,JS输入值是否为判断正整数代码: <script type=”text/javascript”> function test( ...
- Redis学习笔记(1) Redis介绍及基础
1. Redis的特性 (1) 存储结构 Redis(Remote Dictionary Server,远程字典服务器)是以字典结构存储数据,并允许其他应用通过TCP协议读写字典中的内容.Redis支 ...
- Linux学习笔记(3)Linux常用命令之文件处理命令
Linux的命令格式一般为:命令 [-选项] [参数],如ls -la /etc,需要注意几点:1)个别命令使用不遵循此格式:2)当有多个选项时,可以写在一起:3)存在简化选项(-)与完整选项,如-a ...
- 记录Android Studio项目提交到github上的出错处理
首先是按照网上的教程进行了一次提交,具体见http://web.gxzj.com.cn/News.aspx?id=325505 记得当时出现过这个错误Can't connect to reposito ...
- SpringJMS解析1-使用示例
Spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...