• 百度之星 预赛003--字典树


    自己写的代码,到了最后也没有搞懂自己能的指针,到了网上看了些题解,自己的思路也错了,虽然都用的是字典树解法,虽然走了弯路,,但最后也把指针这地方的知识又回顾了一遍吧。。

    也懒得自己打了,明天自己在找个思路做一下吧。。。

    #include <stdio.h>  
    #include <string.h>  
    
    typedef struct node{  
        int num;  
        node *next[30];  
        node(){  
            memset(next,0,sizeof(next));  
            num=0;  //值得一学的好处理方法 简单实用 初始化节点的漂亮代码 
        }  
    }Trie;  
    char dir[32],s[32];  
    void Insert(node *root,char *s){  
        node *p=root;  
        for(int i=0; s[i]; i++){  
            int x=s[i]-'a';  
            if(p->next[x]==NULL) 
            p->next[x]=new node;  
            p=p->next[x];  
            p->num++; 
        }  
    }  
    int Search(node *root,char *s){  
        node *p=root;  
        for(int i=0;s[i];i++){  
            int x=s[i]-'a';  
            if(p->next[x]==NULL) 
            return 0;  
            p=p->next[x];  
        }  
        return p->num;  
    }  
    void Delete(node *root,char *s,int cnt){  
        node *p=root;  
        for(int i=0;s[i];i++){  
            int x=s[i]-'a';  
            p=p->next[x];  
            p->num-=cnt;  //删除节点的操作无非就是把当前前缀开头单词数减掉 
        }  
        for(int i=0;i<30;i++)
        p->next[i]=0;  //再把之后所有的孩子一律杀死(吼吼 好残忍) 
    }  
      
    int main(){  
        int n;  
        scanf("%d",&n);  
        Trie *root=new node;  
        while(n--){  
            scanf("%s %s",dir,s);   
            if(dir[0]=='i'){  //巧妙处理 不用整个串匹配 系统给的指令一定很标准 判定首字母即可 
                Insert(root,s);  
            }else if(dir[0]=='s'){  
                int ans=Search(root,s);  
                if(ans) 
                    printf("Yes
    ");
                else    
                    printf("No
    ");
            }else{  
                int 
                cnt=Search(root,s);  
                if(cnt) 
                    Delete(root,s,cnt);  
            }  
        }  
        return 0;  
    }  

    自己写的错误代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    struct node {
        struct node *next[26];
        node()
        {
            memset(next,NULL,sizeof(next));
        }
    };
    void del(node *&root);
    void buildtrid(node *root,char *s)
    {
        node *p=root;
        node *tmp=NULL;
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(p->next[s[i]-'a']==NULL)
            {
                tmp=new node;
                p->next[s[i]-'a']=tmp;
            }
            p=p->next[s[i]-'a'];
        }
    }
    
    void del1(node *root,char *s)
    {
        node *p=root;
        node *tmp;
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(p->next[s[i]-'a']==NULL)
                return ;
            tmp=p;
            p=p->next[s[i]-'a'];
        }
        del(p);
        delete p;
        tmp->next[s[l-1]-'a']=NULL;    
    }
    
    void del(node *&root)
    {
        node *p=root;
        for(int i=0;i<26;i++)
            if(p->next[i])
                del(p->next[i]);
        delete root;
        root=NULL;
    }
    
    void findtrie(node *root,char *s)
    {
        node *p=root;
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(p->next[s[i]-'a']==NULL)
            {
                printf("No
    ");
                return;
            }
            p=p->next[s[i]-'a'];
        }
        printf("Yes
    ");
    }
    
    int main()
    {
        int n;
        char s[30],s1[30];
        scanf("%d",&n);
        node root;
        while(n--)
        {
            scanf("%s%s",s,s1);
            if(s[0]=='i')
                buildtrid(&root,s1);
            else
                if(s[0]=='d')
                    del1(&root,s1);
                else
                    findtrie(&root,s1);
        }
    }
            
    错误代码
  • 相关阅读:
    hdu4717 The Moving Points(二分做法)
    C++中用rand()和srand()产生随机数方法介绍
    教你看懂C++类库函数定义之一---HRESULT 宏
    [置顶] IOS培训资料
    调试出不来 断点不起作用 调试技巧 MyEclipse进不了调试
    [置顶] 编程模仿boost::function和boost::bind
    模拟红外协议接收程序
    Java 使用JDBC、DBCP、C3P0访问数据库
    Linux点亮一个灯
    Makefile解析(最简单的LED)
  • 原文地址:https://www.cnblogs.com/WDKER/p/5499212.html
Copyright © 2020-2023  润新知