2016"百度之星" - 资格赛(Astar Round1)C
度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
这里仅有一组测试数据。第一行输入一个正整数N (1\leq N\leq 100000)N(1≤N≤100000),代表度熊对于字典的操作次数,接下来NN行,每行包含两个字符串,中间中用空格隔开。第一个字符串代表了相关的操作(包括: insert, delete 或者 search)。第二个字符串代表了相关操作后指定的那个字符串,第二个字符串的长度不会超过30。第二个字符串仅由小写字母组成。
对于每一个search 操作,如果在度熊的字典中存在给定的字符串为前缀的单词,则输出Yes 否则输出 No。
5
insert hello
insert hehe
search h
delete he
search hello
Yes
No
字典树的操作~模版自己加了删除语句
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std; typedef struct Trie{
int v;
Trie *next[];
}Trie;
Trie root;
void createTrie(char *str)
{
int len = strlen(str);
Trie *p = &root, *q;
for(int i=; i<len; ++i)
{
int id = str[i]-'a';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(root));
q->v = ;
for(int j=; j<; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p->next[id]->v++;
p = p->next[id];
}
}
} int findTrie(char *str)
{
int len = strlen(str);
Trie *p = &root;
for(int i=; i<len; ++i)
{
int id = str[i]-'a';
p = p->next[id];
if(p == NULL)
return ;
}
return p->v;
}
void dele(char *str,int n)
{
Trie *p = &root;
int len = strlen(str);
for(int i=; i<len; ++i)
{
int id = str[i]-'a';
p = p->next[id];
p->v-=n;
}
for(int j=; j<; ++j)
{
p->next[j] = NULL;
}
}
int main()
{
int t;
char s1[],s2[];
for(int i=;i<;i++)
{
root.next[i]=NULL;
}
cin>>t;
while(t--)
{
scanf("%s%s",s1,s2);
if(s1[]=='i')
{
createTrie(s2);
}
else if(s1[]=='s')
{
int ans=findTrie(s2);
if(ans)
{
puts("Yes");
}
else
{
puts("No");
}
}
else
{
int cnt=findTrie(s2);
if(cnt)
{
dele(s2,cnt);
}
}
}
return ;
}
2016"百度之星" - 资格赛(Astar Round1)C的更多相关文章
- 2016百度之星 资格赛ABCDE
看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...
- HDU 5688:2016"百度之星" - 资格赛 Problem D
原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5686:2016"百度之星" - 资格赛 Problem B
原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5685:2016"百度之星" - 资格赛 Problem A
原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others) ...
- 2016"百度之星" - 资格赛(Astar Round1)
逆元 1001 Problem A 求前缀哈希和逆元 #include <bits/stdc++.h> typedef long long ll; const int MOD = 9973 ...
- 2016"百度之星" - 资格赛(Astar Round1) 1004
思路:题目很简单,直接用map记录每个字符串的个数就可以了.记得对每个字符串先sort(). AC代码: #include <cstdio> #include <stdlib.h&g ...
- 2016"百度之星" - 资格赛(Astar Round1) 1001
思路:第一个做法就是:每读入起始位置i和结束位置j,就从这位置i到位置j计算,可是TLE了,后面我想想要是我输入一个最长的字符串,且以最大次数计算开始位置1到结束位置100000,那么这计算量是很大的 ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem E
简单模拟题,耐心写就能过. #include <stdio.h> #include <math.h> #include<cstring> #include<c ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem C
字典树. 插入的时候update一下节点出现的次数. delete的时候,先把前缀之后的全删了.然后看前缀最后一个节点出现了几次,然后前缀上每个节点的次数都减去这个次数. 前缀从上到下再检查一遍,如果 ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem D
排个序,map直接搞. #include <stdio.h> #include <math.h> #include<cstring> #include<cma ...
随机推荐
- 配gzip的过滤器进行压缩解决表单加载慢问题
一个客户的表单上字段超过五百,经浏览器的调试器发现主要问题是从服务器取数据花费了大量时间,下载内容大小约1.2M,下载时间在10s左右,导致样式加载完大约在17s左右(不清除浏览器缓存).最终考虑利用 ...
- Mybatis的批处理以及执行Update返回行数为负数
项目中用到了批量更新. 在开发当中,可能经常会遇到批量处理这种情况,一般都再在java层面进行, 其本质是节省数据库连接打开关闭的的次数,占用更少的运行内存. 下面先记一下批处理映射吧: mybati ...
- require()和include()代码重用
第五章 require()函数和include()函数几乎是相同的,二者唯一的区别在于函数失败后,require()函数将给出一个致命的错误,而include()只是给出一个警告. require_o ...
- Node.js的__dirname,__filename,process.cwd(),./的含义
简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...
- ROS Learning-020 learning_tf-04(编程)让turtle2 海龟跟随turtle1海龟,并绕着 turtle1海龟转圈 (Python版)
ROS Indigo learning_tf-04 (编程)让 turtle2 海龟跟随 turtle1 海龟,并绕着 turtle1 海龟转圈 (Python版) 我使用的虚拟机软件:VMware ...
- Mind Map-在线软件(转)
From http://blog.sina.com.cn/s/blog_74b687ac0102dtp1.html 第一款:http://tu.mindpin.com/ 用非常简单, 先用Email ...
- Amazon S3 云服务
一.简介 Amazon Simple Storage Service (S3) 是一个公开的服务,Web 应用程序开发人员可以使用它存储数字资产,包括图片.视频.音乐和文档. S3 提供一个 REST ...
- R: 绘图 pie & hist
问题: 绘制 pie .hist 图 解决方案: 饼图函数 pie( ) pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise ...
- Luogu 1641 [SCOI2010]生成字符串
结果和dp没有一点关系…… 30分算法:设$f_{i, j}$表示已经选了$i$个并且有$j$个是白色的状态数,转移显然,最后答案就是$f_{n + m, m}$,时间复杂度$O(n^{2})$. 1 ...
- 使用Maven搭建SSM框架(Eclipse)
今天学习一下使用Maven搭建SSM框架,以前都是用别人配置好的框架写代码,今天试试自己配置一下SSM框架. 这里我的参数是Windows7 64位,tomcat9,eclipse-jee-neon- ...