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 ...
随机推荐
- MVC4相关Razor语法以及Form表单(转载)
Razor的布局(Layout) 默认建的工程都自带的了一个_ViewStart.cshtml文件,文件里面的代码如下: @{ Layout = "~/Views/Shared/_Layou ...
- Ajax+Spring MVC实现跨域请求(JSONP)JSONP 跨域
JSONP原理及实现 接下来,来实际模拟一个跨域请求的解决方案.后端为Spring MVC架构的,前端则通过Ajax进行跨域访问. 1.首先客户端需要注册一个callback(服务端通过该callba ...
- 经常出现null错误之tostring
如果需要转换的类型可能为null,如果使用tostring就可能引发错误,这时候可以使用convert.tostring方法.
- pulseaudio的交叉编译
在/etc/profile里导入 export PATH==$PATH:/home/jack/arm-linux-gcc/x-tools/arm-unknown-linux-gnueabi/bin 配 ...
- cordova sqlite
jar包在这里下载 https://github.com/litehelpers/Cordova-sqlite-storage 把SQLitePlugin 复制到自己工程目录 org.pgsqlite ...
- MyBatis-xml配置SQL文件中,传入List数组、基本类型String、int……、与自定义类型的方法
//基本类型 @Override public String queryItemNumber(String packId) throws Exception { // TODO Auto-genera ...
- java.sql.ResultSet技术(从数据库查询出的结果集里取列值)
里面有一个方法可以在查询的结果集里取出列值,同理,存储过程执行之后返回的结果集也是可以取到的. 如图: 然后再运用 java.util.Hashtable 技术.把取到的值放入(K,V)的V键值里,K ...
- 关于spring的注解方式注入默认值(转) -- 首字母小写
1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...
- 学习笔记:GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2)
GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2) GLSL 是一种管道,一种图形化的流水线 1.GLSL 的具体工作流程: 简化流程如下: ...
- OC与Swift桥接问题
入职新公司后,接手了一个Swift项目.项目质量已经吐槽过一次就略过了,感兴趣的可以看我之前的博客.当然我之前对Swift只是略有了解,略到只看过没写过的程度,主要语言还是OC.不过嘛其实语言都是相通 ...