码迷,mamicode.com
首页 > 其他好文 > 详细

字典树

时间:2019-04-01 10:28:35      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:val   def   ret   ==   turn   ring   %s   字典   trie   

1
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define me(a,x) memset(a,x,sizeof(a))
struct Trie{
    Trie *next[27];
    int num;
    Trie(){me(next,NULL);num=1;}
};
void insert(char *str,int n,Trie* &root){
    if(str[n]==‘\0‘)return;
    int key=str[n]-‘a‘;
    if(root->next[key]==NULL){
        root->next[key]=new Trie();
        insert(str,n+1,root->next[key]);
    }else {
        (root->next[key]->num)++;
        insert(str,n+1,root->next[key]);
    }
}
int query(char *str,Trie* root){
    Trie* p=root;
    int n=0;
    while(str[n++]){
        int key=str[n-1]-‘a‘;
        if(p->next[key]==NULL)return 0;
        p=p->next[key];
    }
    return p->num;
}
void free_root(Trie* root){
    if(root==NULL)return;
    for(int i=0;i<26;i++)if(root->next[i])free_root(root->next[i]);
    delete(root);
}   
int main(){
    Trie *root=new Trie();
    char str[15];
    while(gets(str)&&str[0])insert(str,0,root);
    while(~scanf("%s",str))printf("%d\n",query(str,root));
    free_root(root);
}

2

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define me(a,x) memset(a,x,sizeof(a))
int Trie[1000005][26]={0};
int num[1000005]={0};
int tot=0;
void insert(char *str){
    int next=0,n=0;
    while(str[n++]){
        int key=str[n-1]-‘a‘;
        if(Trie[next][key]==0)
        Trie[next][key]=++tot;
        num[Trie[next][key]]++;
        next=Trie[next][key];
    }
}
int query(char *str){
    int n=0,next=0;
    while(str[n++]){
        int key=str[n-1]-‘a‘;
        if(Trie[next][key]==0)return 0;
        next=Trie[next][key];
    }return num[next];
}
int main(){
    char str[15];
    while(gets(str)&&str[0])insert(str);
    while(~scanf("%s",str))printf("%d\n",query(str));
}

01Trie

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof(a))
struct Trie{
    int val;
    Trie *next[2];
    Trie(){
        val=-1;
        next[0]=next[1]=NULL;
    }
};
void insert(Trie* &root,int x,int bit){
    if(bit==-1){root->val=x;return;}
    int key=(1ll*x>>bit)&1;
    if(root->next[key]==NULL)root->next[key]=new Trie();
    insert(root->next[key],x,bit-1);
}
int query(Trie* root,int x,int bit){
    if(bit==-1)return root->val;
    int key=(1ll*x>>bit)&1;
    if(root->next[key^1]!=NULL)return query(root->next[key^1],x,bit-1);
    else return query(root->next[key],x,bit-1);
}
int main(){
    int t;cin>>t;
    for(int cas=1;cas<=t;cas++){
        int n,m;
        scanf("%d%d",&n,&m);
        Trie *root=new Trie();
        for(int i=0;i<n;i++){
            int u;scanf("%d",&u);
            insert(root,u,32);
        }
        printf("Case #%d:\n",cas);
        while(m--){
         int u;scanf("%d",&u);
         printf("%d\n",query(root,u,32));
        }
    }
}

字典树

标签:val   def   ret   ==   turn   ring   %s   字典   trie   

原文地址:https://blog.51cto.com/14093713/2372110

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!