hdu5687
来源:互联网 发布:sqlserver case when 编辑:程序博客网 时间:2024/06/15 15:54
度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
5insert helloinsert hehesearch hdelete hesearch hello
YesNo
AC代码:
#include<stdio.h>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<cstring>#include<string.h>#include<queue>#include<stack>#include<cmath>#include<set>#include<map>#include<string>using namespace std;const int N=(1e5+10)*30;typedef long long ll;int top;//top表示节点个数int tree[N][27];//字典树int val[N];//记录某个节点出现的次数void Insert(char *s){ int len=strlen(s); int u=0; for(int i=0;i<len;i++) { int c=s[i]-'a'; if(!tree[u][c]) { memset(tree[top],0,sizeof tree[top]); val[top]=1; tree[u][c]=top++; } else { int x=tree[u][c]; val[x]++; } u=tree[u][c]; }}int Search(char *str){ int len=strlen(str); int u=0; for(int i=0;i<len;i++) { int c=str[i]-'a'; u=tree[u][c]; if(!u) return -1; } return val[u];}void Delete(char *str,int v){ int len=strlen(str); int u=0; for(int i=0;i<len;i++) { int c=str[i]-'a'; u=tree[u][c]; val[u]-=v; } memset(tree[u],0,sizeof tree[u]);}int main(){ int n; scanf("%d",&n); top=1; memset(tree[0],0,sizeof tree[0]); while(n--) { char op[10],str[40]; scanf("%s %s",op,str); if(op[0]=='i') Insert(str); else if(op[0]=='s') { if(Search(str)>0) printf("Yes\n"); else printf("No\n"); } else { int v=Search(str); if(v>0) { Delete(str,v); } } } return 0;}
阅读全文
1 0
- HDU5687
- hdu5687
- hdu5687
- HDU5687-Problem C
- HDU5687(字典树Trie增删查)
- 2016"百度之星" - 资格赛(Astar Round1)(hdu5685(线段树、乘法逆元),hdu5686(大数),hdu5687(字典树),hdu5688)
- java定时scheduledExecutionTime
- 中文文本处理传统方法
- Time To Get Up
- hdu 1081 1559 最大子矩阵
- Learning Python Part I 之 列表
- hdu5687
- MyBatis Generator系列(二)----自定义CommentGenerator实现中文注释
- markdown查阅
- Webpack安装extract-text-webpack-plugin遇见的两个问题
- Gym 101490F Endless Turning 半平面交
- Mysql-存储过程
- 《着色器和屏幕特效》读书笔记第五章-顶点函数
- linux chmod和fchmod设置 文件和目录权限设置
- 最大合成问题