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

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

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

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

分析 : 利用字典树,插入操作时对于每一个节点的标记权值+1,代表此前缀+1,然后删除操作的时候尤为要注意的就是对于给出的前缀,不能在字典树上将权值置为0,因为如果现在假设已经插入了 abc、ab 现在要求 delete abc 则不能将 ab 这个前缀的标记权值变成 0 ,这很显然是不对的( 虽然自己傻逼被坑了=_= ),正确的做法是找到 abc 在字典树出现的次数,然后再从 root 节点将 abc 这个前缀途经的节点减去这个次数,最后面对 abc 后面的节点 free 掉即可

#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
;

struct Trie
{
    Trie *Next[maxn];
    int flag;
    inline void init(){
        ;
        ; i<maxn; i++)
            this->Next[i] = NULL;
    }
};
Trie *root = (Trie *)malloc(sizeof(Trie));

inline void DelTrie(Trie *T)
{
    if(T == NULL) return ;
    ; i<maxn; i++){
        if(T->Next[i] != NULL)
            DelTrie(T->Next[i]);
    } free(T);
    return ;
}

void CreateTrie(char *str, bool isDel)
{
    int len = strlen(str);
    Trie *p = root, *tmp;
    ;
    ; i<len; i++){
        int idx = str[i]-'a';
        if(!isDel){
            if(p->Next[idx] == NULL){
                tmp = (Trie *)malloc(sizeof(Trie));
                tmp->init();
                p->Next[idx] = tmp;
                p = p->Next[idx];
            }; p = p->Next[idx]; }
        }else{
            if(p->Next[idx] != NULL){
                DelNum = p->Next[idx]->flag;
                p = p->Next[idx];
            }else return ;
        }
    }
    if(isDel){
        p = root;
        ; i<len; i++){
            int idx = str[i] - 'a';
            if(p->Next[idx] == NULL) return ;
            ){
               DelTrie(p->Next[idx]);
               p->Next[idx] = NULL;
               return ;
            }
            p->Next[idx]->flag -= DelNum;
            p = p->Next[idx];
        }
    }
}

bool FindTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root;
    ; i<len; i++){
        int idx = str[i]-'a';
        ) p = p->Next[idx];
        else return false;
    }return true;
}

int main(void)
{
    root->init();
    int n;
    scanf("%d", &n);
    ], str[];
    while(n--){
        scanf("%s", command);
        ){
            scanf("%s", str);
            CreateTrie(str, false);
        }
        ){
            scanf("%s", str);
            CreateTrie(str, true);
        }
        else{
            scanf("%s", str);
            FindTrie(str)?puts("Yes"):puts("No");
        }
    }DelTrie(root);
    ;
}

瞎 : 要冷静分析啊!字典树上是怎么在跑的,还有内存问题啊!很裸的字典树写不出来就傻逼了!

HDU 5687 Problem C ( 字典树前缀增删查 )的更多相关文章

  1. hdu 5687 Problem C trie树

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Prob ...

  2. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  3. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

  4. HDU 5687 Problem C 【字典树删除】

    传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...

  5. HDU 5687 Problem C(Trie+坑)

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  6. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  9. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

随机推荐

  1. 【HANA系列】SAP HANA ODBC error due to mismatch of version

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA ODBC er ...

  2. 深入理解java:2.3.1. 并发编程concurrent包 之Atomic原子操作(循环CAS)

    java中,可能有一些场景,操作非常简单,但是容易存在并发问题,比如i++, 此时,如果依赖锁机制,可能带来性能损耗等问题, 于是,如何更加简单的实现原子性操作,就成为java中需要面对的一个问题. ...

  3. 如何在centos7中显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意字符的文件或目录

    ls /etc |grep "^[^[:alpha:]][[:alpha:]].*"

  4. XSS注入常用语句(整理)

    <script>alert('hello,gaga!');</script> //经典语句,哈哈! >"'><img src="javas ...

  5. 关于Logcat

    1 android logcat api Log.i(String tag, String msg) info,普通信息 Log.d(String tag, String msg) debug,调试信 ...

  6. 在git bash 中配置git用户名和邮箱及查看配置信息

    Administrator@LuoTong- MINGW32 ~ $ git config --global user.name "mrluotong" Administrator ...

  7. Android的Monkey和MonkeyRunner

    本文部分解释性语段摘自网络百科或其它BLOG,语句内容网络随处可见,也不知道谁是初始原创,便不再署名出处,如有雷同,还请见谅. Monkey 什么是Monkey Monkey是Android中的一个命 ...

  8. QT DBUS: Not connected to D-Bus server, 注意source /etc/profile

    运行环境:ARM 运行如下代码: QDBusConnection bus = QDBusConnection::sessionBus(); if(!bus.registerService(" ...

  9. There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.

    There are multiple modules with names that only differ in casing.This can lead to unexpected behavio ...

  10. MongoDB的使用学习之(四)权限设置--用户名、密码、端口==

    本文参照:http://hi.baidu.com/tianhuimin/item/590d96cfd7ac1509c610b26a 本人也是按照此文章操作的,但是有些不妥,红色文字就是我实践后,需要改 ...