2016"百度之星" - 资格赛(Astar Round1) Problem C
字典树。
插入的时候update一下节点出现的次数。
delete的时候,先把前缀之后的全删了。然后看前缀最后一个节点出现了几次,然后前缀上每个节点的次数都减去这个次数。
前缀从上到下再检查一遍,如果出现了次数为0的节点,也删去。
#include <stdio.h>
#include <math.h>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<algorithm>
using namespace std; struct Node
{
int val;
int f[];
} node[]; int root,n;
char op[], s[];
int tot; int newnode()
{
++tot;
node[tot].val=;
for(int i=; i<; i++) node[tot].f[i]=-;
return tot;
} void Insert()
{
int p=root;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-) node[p].f[s[i]-'a']=newnode();
p=node[p].f[s[i]-'a'];
node[p].val++;
}
} void Delete()
{
int p=;
bool Find=;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-)
{
Find=;
break;
}
p=node[p].f[s[i]-'a'];
}
if(Find==) return; for(int i=; i<; i++) node[p].f[i]=-; int Val=node[p].val; p=root;
for(int i=; s[i]; i++)
{
p=node[p].f[s[i]-'a'];
node[p].val=node[p].val-Val;
} p=;
for(int i=; s[i]; i++)
{
int tmp=node[p].f[s[i]-'a'];
if(node[tmp].val==)
{
node[p].f[s[i]-'a']=-;
break;
}
p=node[p].f[s[i]-'a'];
}
} void Search()
{
int p=root; bool flag=;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-)
{
flag=;
break;
}
p=node[p].f[s[i]-'a'];
} if(flag) printf("No\n");
else printf("Yes\n");
} int main()
{
while(~scanf("%d",&n))
{
tot=; root=;
for(int i=; i<; i++) node[root].f[i]=-;
for(int i=; i<=n; i++)
{
scanf("%s%s",op,s);
if(strcmp(op,"insert")==) Insert();
else if(strcmp(op,"delete")==) Delete();
else if(strcmp(op,"search")==) Search();
}
}
return ;
}
2016"百度之星" - 资格赛(Astar Round1) Problem C的更多相关文章
- 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百度之星 资格赛ABCDE
看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem E
简单模拟题,耐心写就能过. #include <stdio.h> #include <math.h> #include<cstring> #include<c ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem D
排个序,map直接搞. #include <stdio.h> #include <math.h> #include<cstring> #include<cma ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem B
规律题,斐波那契数列,数据有毒,0的时候输出换行.会爆longlong,写个大数模板或者Java搞. import java.io.BufferedInputStream; import java.m ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem A
保存前缀乘,询问的时候输出c[ri]/c[li-1]即可,因为是除法,所以计算一下c[li-1]的逆元. #include <stdio.h> #include <math.h> ...
- 2016"百度之星" - 资格赛(Astar Round1)
逆元 1001 Problem A 求前缀哈希和逆元 #include <bits/stdc++.h> typedef long long ll; const int MOD = 9973 ...
随机推荐
- svn rollback: 恢复到上一版本
18:48:32svn的文件版本是168,我想用167的版本覆盖掉168的版本如何搞? 18:52:47先把本地的那个文件用rm命令删掉,然后,使用svn up -r 167 文件路径,UP下来的文件 ...
- 【读书笔记】C Primer Plus ch.15位运算 示例程序15.1 整数转换成二进制字符串
正文: https://www.zybuluo.com/RayChen/note/595213
- iOS 上架提示ipad需要显示四个方位,而我们只能竖屏的时候的解决办法
勾选requires sull screen
- eclipse关联源码的方法
1.在项目的libs目录下,新建一个android-support-v4.jar.properties文件 2.打开android-support-v4.jar.properties,编辑. 输入源码 ...
- myeclipse 调试JSP页面
http://jingyan.baidu.com/article/636f38bb1ef1aad6b9461048.html
- auto_ptr浅析
auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...
- How to Iterate Over a Map in Java?(如何遍历Map)
1.Iterate through the "entrySet" like so: public static void printMap(Map mp) { Iterator i ...
- 最短路<dijk>
题意: 有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路.按格式输出. power oj 2443 题解: 标准dijk算法. #include<c ...
- ACE_Time_Value
头文件“Time_Value.h” 为了兼容各个平台的时间特性,ACE Reactor框架提供了ACE_Time_Value类.ACE_Time_Value的关键方法见下图3.2和表3.2.3.3: ...
- iOS5新特性: Core Image 示例
iOS5给我们带来了很多很好很强大的功能和API.Core Image就是其中之一,它使我们很容易就能处理图片的各种效果,色彩啊,曝光啊,饱和度啊,变形啊神马的. 可惜苹果一直没能完善官方文档,也没有 ...