2016年百度之星C

Problem C

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 284    Accepted Submission(s): 96


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

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

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

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

Input
这里仅有一组测试数据。第一行输入一个正整数 N(1N100000) ,代表度熊对于字典的操作次数,接下来 N 行,每行包含两个字符串,中间中用空格隔开。第一个字符串代表了相关的操作(包括: 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
 

Source
 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
const int N = 3e6 + 10;
struct Node
{
    void init()
    {
        val = 0;
        memset(next,-1,sizeof next);
    }
    int val;
    int next[26];
}treeRoot[N];
int tot;

void init()
{
    tot = 0;
}

void dfs(int pos)
{
    if(treeRoot[pos].val == 1)
        treeRoot[pos].val = -1;
    for(int i = 0; i < 26; ++i)
    {
        if(treeRoot[pos].next[i] != -1)
        {
            int tp = treeRoot[pos].next[i];
            dfs(tp);
        }
    }
}

void insert_string(char *s)
{
    int pos = 0;
    for(int i = 0; s[i]; ++i)
    {
        int c = s[i] - 'a';
        if(treeRoot[pos].next[c] == -1)
        {
            treeRoot[pos].next[c] = ++tot;
            treeRoot[tot].init();
        }
        pos = treeRoot[pos].next[c];
    }
    treeRoot[pos].val = 1;
}

bool dfsAgain(int pos)
{
    if(treeRoot[pos].val == 1)
        return true;
    bool isTrue = false;
    for(int i = 0; i < 26 && !isTrue; ++i)
    {
        if(treeRoot[pos].next[i] != -1)
        {
            int tp = treeRoot[pos].next[i];
            isTrue = dfsAgain(tp);
        }
    }
    return isTrue;
}

bool find_preString(char *s)
{
    int pos = 0;
    for(int i = 0; s[i]; ++i)
    {
        int c = s[i] - 'a';
        if(treeRoot[pos].next[c] == -1)
            return false;
        pos = treeRoot[pos].next[c];
    }
    return dfsAgain(pos);
}

void delete_string(char *s)
{
    int pos = 0;
    for(int i = 0; s[i]; ++i)
    {
        int c = s[i] - 'a';
        if(treeRoot[pos].next[c] == -1)
            return;
        pos = treeRoot[pos].next[c];
    }
    dfs(pos);
}

char s[40],t[40];

int main()
{
    int n;
    cin >> n;
    treeRoot[0].init();
    init();
    for(int i = 0; i < n; ++i)
    {
        scanf("%s %s",t,s);
        if(!strcmp(t,"insert")) insert_string(s);
        else if(!strcmp(t,"search")) puts(find_preString(s) ? "Yes" : "No");
        else
        {
            delete_string(s);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值