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: ...
随机推荐
- redis 常用命令
查看redis信息和状态: > info redis下,数据库是由一个整数索引标识,而不是由一个数据库名称.默认情况下,一个客户端连接到数据库0.redis配置文件中下面的参数来控制数据库总数: ...
- MVC - 10.CodeFrist
微软示例 1.(对新数据库使用 Code First):http://msdn.microsoft.com/zh-cn/data/jj193542 2.(连接和模型):http://msdn.micr ...
- Ext Js【Hello World】 ——4.1 beta 1
准备:vs+ExtJs4.1Beta1 ExtJS 4.1 xiazai_ https://yunpan.cn/cqv6bdBwtRjAj (提取码:2733) 引用,cs文件,js主入口,zh—c ...
- Pyqt清空Win回收站
Pyqt清空回收站其实的调用Python的第三方库,通过第三方库调用windows的api删除回收站的数据 一. 准备工作 先下载第三方库winshell 下载地址: https://github.c ...
- C#在excel中添加超链接
1.新建一个项目 2.给项目添加引用:Microsoft Excel 12.0 Object Library (2007版本) using Excel = Microsoft.Office.Inter ...
- c中malloc的用法
转自:http://blog.sina.com.cn/s/blog_966f8e8501010if7.html Malloc 向系统申请分配指定size个字节的内存空间.返回类型是 void* 类型. ...
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...
- World of Warcraft
一大堆快捷键 鼠标中键:致盲 上滚:闪避 下滚:疾跑 通用:~伺机待发 Q加速 E斗篷 R徽章 T消失 alt+Q脚踢,alt+W毒刃 alt+E亡灵意志 alt+D烟幕弹 alt+F死亡标记 Z天降 ...
- 退出Activity(转)
退出Activity 如何退出Activity?如何安全退出已调用多个Activity的Application? 退出activity 直接调用 finish () 方法 . //用户点击back键 ...
- 在Salesforce中通过dataloadercliq调用data loader来批量处理数据
上一篇文章讲到,通过data loader去批量处理数据,那么这篇文章将主要讲解在Salesforce中通过dataloadercliq调用data loader来批量处理数据. 1): CLIq文件 ...