HDOJ5687 字典树模板,数组实现
来源:互联网 发布:java排队叫号 编辑:程序博客网 时间:2024/06/01 10:12
Problem C
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1673 Accepted Submission(s): 482
Problem Description
度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
Input
这里仅有一组测试数据。第一行输入一个正整数N(1≤N≤100000) ,代表度熊对于字典的操作次数,接下来N 行,每行包含两个字符串,中间中用空格隔开。第一个字符串代表了相关的操作(包括: insert, delete 或者 search)。第二个字符串代表了相关操作后指定的那个字符串,第二个字符串的长度不会超过30。第二个字符串仅由小写字母组成。
Output
对于每一个search 操作,如果在度熊的字典中存在给定的字符串为前缀的单词,则输出Yes 否则输出 No。
Sample Input
5insert helloinsert hehesearch hdelete hesearch hello
Sample Output
YesNo
Source
2016"百度之星" - 资格赛(Astar Round1)
Recommend
liuyiding | We have carefully selected several similar problems for you: 6107 6106 6105 6104 6103
题意大家都懂的,就是个很裸的字典树题目。现在用数组写一个模板程序,供参考。
#include <iostream>#include <cstdio>#include <ctime>#include <algorithm>#include <cstring>using namespace std;const int maxn =3e6+10; //命令行数是100000,字符串长度不超过30,上限就是3e6struct node { int next[26]; //只考虑小写字母的情况,大写的话扩大到52,有数字的话扩大到62 int sum; void init(){ sum=0; memset(next,-1,sizeof(next)); }};node word[maxn];int tot=0;void add(char a[], int len){ int now=0; for (int i=0; i<len;i++){ int tmp=a[i]-'a'; if (word[now].next[tmp]==-1){ int x=tot++; word[x].init(); word[now].next[tmp]=x; } now=word[now].next[tmp]; word[now].sum+=1; }}int query(char a[] , int len){ int now = 0; for (int i=0;i<len;i++){ int tmp=a[i]-'a'; if (word[now].next[tmp]==-1) return 0; now = word[now].next[tmp]; } return word[now].sum;}void del(char a[] , int len , int cnt){ int now=0; for (int i=0;i<len;i++){ int tmp=a[i]-'a'; if (word[now].next[tmp]==-1) return; word[now].sum-=cnt; now=word[now].next[tmp]; } word[now].sum=0; //字符串最后一个字母对应的节点也要清除 for (int i=0;i<26;i++) word[now].next[i]=-1;}int main(){ std::ios::sync_with_stdio(false); //提高读入效率 int n; char str[10],s[40]; cin>>n; tot=1; while (n--){ cin>>str>>s; if (str[0]=='s'){ int ans=query(s,strlen(s)); if (ans) cout<<"Yes"<<endl; else cout<<"No"<<endl; } else if (str[0]=='i'){ add(s,strlen(s)); } else { int ans = query(s,strlen(s)); del(s,strlen(s),ans); } } return 0;}
阅读全文
0 0
- HDOJ5687 字典树模板,数组实现
- 数组字典树模板
- 字典树模板(数组实现和指针实现)
- 【字典树模板(数组实现)】HDU 1251 统计难题
- poj 2001 字典树入门题(数组实现模板)
- 字典树数组实现
- 字典树的指针模板与数组模板
- 【Trie】Trie字典树模板 静态指针池、数组写法
- HDU1075静态数组实现字典树
- 字典树 模板
- 字典树【模板】
- 字典树模板
- 字典树模板
- 字典树模板
- 经典字典树模板
- hdu1247 字典树模板
- 字典树模板
- 字典树模板
- Induction to Vue.js
- 模态框功能实现
- tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your
- 大规模并发带来的挑战
- Linux工具--Xshell & XFtp使用
- HDOJ5687 字典树模板,数组实现
- 闭包例子
- 二分图最大匹配
- Construct Binary Tree from Inorder and Postorder Traversal问题及解法
- android报错之.OutOfMemoryError
- SpringMVC中注解方式代替xml配置SpringMVC
- Java多线程
- python自然语言处理(二)
- 系统栈的工作原理