百度之星 预赛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);
    }
}
        
错误代码
原文地址:https://www.cnblogs.com/WDKER/p/5499212.html