2016"百度之星" - 资格赛 Problem C

Problem Description

度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:

1、insert : 往神奇字典中插入一个单词

2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词

3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
Input

这里仅有一组测试数据。第一行输入一个正整数N (1\leq N\leq 100000)N(1N100000),代表度熊对于字典的操作次数,接下来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




解题思路:裸tire,没什么好说的。



/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define mod 1000000007
#define ll long long
#define ull unsigned long long

struct tire
{
    tire *word[26];
    int cnt;
    tire()
    {
        cnt=0;
        for(int i=0;i<26;i++)
            word[i]=NULL;
    }
};

void build(tire *rt,char *str)
{
    int i;
    for(i=0;str[i]!='\0';i++)
    {
        if(!rt->word[str[i]-'a'])
        {
            rt->word[str[i]-'a']=new tire();
        }
        rt->word[str[i]-'a']->cnt++;
        rt=rt->word[str[i]-'a'];
    }
}

bool findtire(tire *rt,char *str)
{
    int i;
    for(i=0;str[i]!='\0';i++)
    {
        if(rt->word[str[i]-'a'])
            rt=rt->word[str[i]-'a'];
        else
            break;
    }
    if(str[i]=='\0')
    {
        if(rt->cnt>0)
            return true;
    }
    return false;
}

void del(tire *rt,char *str)
{
    tire *head,*tail;
    head=rt;
    int num,i;
    for(i=0;str[i]!='\0';i++)
    {
        if(rt->word[str[i]-'a'])
        {
            tail=rt;
            num=rt->word[str[i]-'a']->cnt;
            rt=rt->word[str[i]-'a'];
        }
        else
            break;
    }
    if(str[i]=='\0')
    {
        free(tail->word[str[i-1]-'a']);
        tail->word[str[i-1]-'a']=NULL;
        for(int j=0;str[j]!='\0';j++)
        {
            if(head->word[str[j]-'a']!=NULL)
            {
                head->word[str[j]-'a']->cnt-=num;
                head=head->word[str[j]-'a'];
            }

        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    tire *rt=new tire();
	int n;
	scanf("%d",&n);
	while(n--)
    {
        char s[35],order[10];
        scanf("%s %s",order,s);
        if(order[0]=='i')
        {
            build(rt,s);
        }
        else if(order[0]=='d')
        {
            del(rt,s);
        }
        else
        {
            if(findtire(rt,s))
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
        }
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值