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 ...
随机推荐
- java GC是在什么时候,对什么东西,做了什么事情?
1.新生代有一个Eden区和两个survivor区,首先将对象放入Eden区,如果空间不足就向其中的一个survivor区上放,如果仍然放不下就会引发一次发生在新生代的minor GC,将存活的对象放 ...
- python的print字符串的输出
字符串的输出使用print语句,在每个语句的输出的时候我们使用' '和" "来包含字符串比如: 如果有多个字符串的话呢我们需要用”,“来进行连接: 我们不仅可以使用字符来进行输出时 ...
- javascript的概述
JavaScript是怎么诞生的???刚开始的是为了验证表单而开发出来的. 什么是JavaScript???a.面向对象的编程语言b.解释性的编程语言(说白了就是不用编译的一种语言)c.脚本语言(说白 ...
- activity的四种加载模式介绍
四种加载模式的介绍: a) Standard : 系统默认模式,一次跳转即会生成一个新的实例: b) SingleTop : 和 standard 类似,唯一的区别就是当跳转的对象是位于栈顶 ...
- Spring 第一个程序-Bean的定义和注册
定义一个实现接口的方法 创建xml文件,这个xml文件就是个所谓的容器 不使用spring容器和使用spring容器创建对象执行代码的区别如下: 下面说一下ApplicationC ...
- 42、生鲜电商平台-商品的spu和sku数据结构设计与架构
说明:Java开源生鲜电商平台中商品的spu和sku数据结构设计与架构,包括数据库图标与架构分析. 1. 先说明几个概念. 电商网站采用在商品模块,常采用spu+sku的数据结构算法,这种算法可以将商 ...
- Android消息传递之EventBus 3.0
Android消息传递之EventBus 3.0使用详解 http://www.cnblogs.com/whoislcj/p/5595714.html EventBus 3.0进阶:源码及其设计模式 ...
- LINQ to SQL连接数据库及语句
http://www.cnblogs.com/fengzheng126/archive/2012/04/20/2460620.html
- HTTP 协议 -- 浏览器缓存机制
浏览器缓存机制 浏览器缓存机制主要是 HTTP 协议定义的缓存机制. HTTP 协议中有关缓存的缓存信息头的关键字有 Cache-Control,Pragma,Expires,Last-Modifie ...
- A. Theatre Square(math)
A. Theatre Square time limit per test 1 second memory limit per test 256 megabytes input standard in ...