zoukankan      html  css  js  c++  java
  • 百度之星 预赛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);
        }
    }
            
    错误代码
  • 相关阅读:
    关于session的记录
    关于<input type="hidden"/>标签的记录
    H3C S5120V2-SI 交换机配置
    Windows 系列GVLK密钥
    给因特尔S2600CO服务器主板安装【SAS控制器】驱动
    EMQ消息队列初体验
    泰国佛历的换算问题
    linux下批量查找UTF-8的BOM文件,并去除BOM
    菲律宾Globe/TM卡最省钱的上网方案
    Windows计划任务实现MYSQL冷备份
  • 原文地址:https://www.cnblogs.com/WDKER/p/5499212.html
Copyright © 2011-2022 走看看