Astar2016-Round1 Problem C(字典树)

FROM:

2016"百度之星" - 资格赛(Astar Round1)

http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=690&pid=1003

Problem Description

度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:
1、insert : 往神奇字典中插入一个单词
2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词
3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串

Input
这里仅有一组测试数据。第一行输入一个正整数N (1\leq N\leq 100000)N(1≤N≤100000),代表度熊对于字典的操作次数,接下来NN行,每行包含两个字符串,中间中用空格隔开。第一个字符串代表了相关的操作(包括: insert, delete 或者 search)。第二个字符串代表了相关操作后指定的那个字符串,第二个字符串的长度不会超过30。第二个字符串仅由小写字母组成。

Output
对于每一个search 操作,如果在度熊的字典中存在给定的字符串为前缀的单词,则输出Yes 否则输出 No。

Sample Input
5
insert hello
insert hehe
search h
delete he
search hello
Sample Output
Yes
No

分析

显而易见用trie树,但是自己第一次写代码时出了好几处错误。

一是想修改某节点中值,但误修改为和这个节点中指针值相同的另一个指针变量,就算对这个变量使用引用也是错误的,还是不会修改到节点中值。

二是我的节点错误的定义为:

struct Tree{
	Tree * t[26];
	Tree(){ ... }
};
当测试用例为:

insert hehe 
insert hehehe 
delete hehe 
search heh 
search返回的是查找到,因为在树中删除了hehe为前缀的串,但保留了heh。

代码 (写的太丑了...)

#include <iostream>
using namespace std;
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#define EACH(it,a) for(auto it=begin(a);it!=end(a);it++)
#define LL long long
#define INF 0x3f3f3f3f
const int SZ=100002;

struct Tree{
	Tree * t[26];
	int n;
	Tree(){
		for(int i=0;i<26;i++)
			t[i]=NULL;
		n=0;
	}
};

Tree * root=new Tree;

void insert(char * str)
{
	Tree * p=root;
	p->n++;
	for(int i=0;str[i];i++){
		if( !p->t[str[i]-97] ){
			Tree * tmp=new Tree;
			p->t[str[i]-97]=tmp;
		}
		p=p->t[str[i]-97];
		p->n++;
	}
}

Tree * search(char * str)
{
	Tree * p=root;
	for(int i=0;str[i];i++){
		if(p->t[str[i]-97])
			p=p->t[str[i]-97];
		else
			return NULL;
	}
	return p;
}

int del(Tree * &p)
{
	if(p==NULL)
		return 0;
	for(int i=0;i<26;i++){
		del(p->t[i]);
	}
	int r=p->n;
	delete p;
	p=NULL;
	return r;
}

Tree * deln(char * str,int n)
{
	Tree * p=root;
	for(int i=0;str[i];i++){
		p->n-=n;
		if(p->t[str[i]-97] && p->t[str[i]-97]->n<=n){
			del(p->t[str[i]-97]);
			break;
		}
		p=p->t[str[i]-97];
	}
	return p;
}

void del(char * str)
{
	Tree * p;
	if( p=search(str) ){
		int n=del(p);
		int l=strlen(str);
		char tmp=str[l-1];
		str[l-1]='\0';
		p=search(str);
		p->t[tmp-97]=NULL;
		deln(str,n);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
#ifdef LJY
	freopen("in.txt","r",stdin);
#endif
	int i,j;
	int n;
	char op[8],str[32];
	cin>>n;
	while(n--){
		cin>>op>>str;
		if(!strcmp(op,"insert")){
			insert(str);
		}else if(!strcmp(op,"delete")){
			del(str);
		}else if(!strcmp(op,"search")){
			if(search(str))
				cout<<"Yes";
			else
				cout<<"No";
			cout<<endl;
		}else{
			cout<<"error!"; exit(1);
		}
	}

	return 0;
}

不使用字典树,而使用map存储也能AC这题,代码如下:

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;

map<string, int> mp;
map<string, int>::iterator iter;
char s1[15], S2[31];

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	scanf("%d", &n);
	for (int i=0; i<n; i++) {
		scanf("%s%s", s1, S2);
		string s2 = S2;
		if (s1[0] == 'i')
			mp[s2]++;
		else if (s1[0] == 's'){
			bool f = false;
			for (iter=mp.lower_bound(s2); iter!=mp.end(); iter++) {
				string s = iter->first;
				int cnt = iter->second;
				if (s.find(s2) != 0){ //从0下标开始不匹配
					break;
				}
				if (cnt) {
					f = true; break;
				}
			}
			puts(f ? "Yes": "No");
		}else {
			for (iter=mp.lower_bound(s2); iter!=mp.end(); iter++) {
				string s = iter->first;
				if (s.find(s2) != 0){
					break;
				}
				iter->second = 0;
			}
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值