首页 技术 正文
技术 2022年11月11日
0 收藏 868 点赞 2,597 浏览 1218 个字

题意:中文题;

解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个次数,最后节点的儿子全部归零;

最开始我是只考虑的最后节点,中间节点都没考虑,这样会出现一个问题就是:插入abdefg,删除abcd的时候,查询abc还是会有答案,所有中间过程也得减去;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1200500
using namespace std;
int
trie[maxn][];
int
num[maxn]={-};
int
root;
int
tot;
int
t,n;
void
init()
{

memset(trie,,sizeof(trie));
memset(num,,sizeof(num));
tot=;
}

void
get_trie(char *s)
{

root=;
int
slen=strlen(s);
for
(int i=;i<slen;i++)
{

int
id=s[i]-'a';
if
(!trie[root][id])
{

trie[root][id]=++tot;
}

root=trie[root][id];
num[root]++;
}
}

int
query(char *s)
{

root=;
int
slen=strlen(s);
for
(int i=;i<slen;i++)
{

int
id=s[i]-'a';
if
(!trie[root][id])
{

return
;
}

root=trie[root][id];
}

return
num[root];
}

void
delete_trie(char *s)
{

root=;
int
cnt=;
int
slen=strlen(s);
for
(int i=;i<slen;i++)
{

int
id=s[i]-'a';
if
(!trie[root][id])
{

return
;
}

root=trie[root][id];
}

cnt=num[root];
root=;
for
(int i=;i<slen;i++)
{

int
id=s[i]-'a';
root=trie[root][id];
num[root]=num[root]-cnt;
}

for
(int i=;i<=;i++)
trie[root][i]=;
}

int
main()
{

char
s[],a[];
scanf("%d",&t);
init();
while
(t--)
{

scanf("%s",s);
scanf("%s",a);
if
(s[]=='i')
{

get_trie(a);
}

else if
(s[]=='s')
{

int
flag=query(a);
if
(flag==)
printf("No\n");
else

printf("Yes\n");
}

else

{

delete_trie(a);
}
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,498
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,911
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,745
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,499
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,138
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,302