首页 > 其他 > 详细

字典树

时间:2017-04-12 04:14:00      阅读:150      评论:0      收藏:0      [点我收藏+]

HDU 1251

就裸的字典树了

技术分享
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define ll   __int64
#define MAXN 300010
#define inf  1000000007

struct node
{
    struct node* next[27];
    int cnt;
};

struct node *root;

void Init(struct node *p)
{
    p->cnt=0;
    memset(p->next,0,sizeof(p->next));
    //printf("1\n");
}
char s[15];

void Insert(char *s1,struct node *p)
{
    while(*s1)
    {
        int a=*s1-a;
        if(p->next[a]==0)
        {
            p->next[a]=(struct node *)malloc(sizeof(struct node));
            Init(p->next[a]);
        }
        p=p->next[a];
        (p->cnt)++;
        s1++;
    }
}
int Ques(char *s1,struct node *p)
{
    while(*s1)
    {
        int a=*s1-a;
        if(p->next[a]==0)
        {
            return 0;
        }
        p=p->next[a];
        s1++;
    }
    return p->cnt;
}
int main()
{
    root=(struct node*)malloc(sizeof(struct node));
    Init(root);
    while(1)
    {
        gets(s);
        Insert(s,root);
        if(strlen(s)==0)
            break;
    }
    while(scanf("%s",s)!=EOF)
    {
        int ans=Ques(s,root);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

HDU 4825

字典树 根据二进制的0 1 建树 从头开始建

比如说5    那么是00000000000000101  总共32位  要补上0  因为从尾到头建就不一定对了

然后看代码

技术分享
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define ll   __int64
#define MAXN 100010
#define inf  1000000007

int cnt;
struct node
{
    int l,r;
    void Init()
    {
        l=r=-1;
    }
}x[MAXN*35];

void Insert(ll a,int root)
{
    int p=root;

    for(int i=32;i>=0;i--)
    {
        if(a&(1ll<<i))
        {
            if(x[p].r==-1)
            {
                x[p].r=++cnt;
                x[cnt].Init();
            }
            p=x[p].r;
        }
        else
        {
            if(x[p].l==-1)
            {
                x[p].l=++cnt;
                 x[cnt].Init();
            }
            p=x[p].l;
        }

    }
}
ll Ques(ll a,int root)
{
    int p=root;
    ll ans=0;
    for(int i=32;i>=0;i--)
    {
        if(a&(1ll<<i))
        {
            if(x[p].l==-1)
            {
                ans=ans<<1;
                p=x[p].r;
            }
            else
            {
                 ans=ans<<1|1;
                 p=x[p].l;
            }

        }
        else
        {
            if(x[p].r==-1)
            {
                ans=ans<<1;
                p=x[p].l;
            }
            else
            {
                 ans=ans<<1|1;
                 p=x[p].r;
            }
        }
    }
    return ans;
}


int main()
{
    int t,ca;
    scanf("%d",&t);
    ca=1;
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        cnt=0;
        x[0].Init();
        for(int i=1;i<=n;i++)
        {
            ll a;
            scanf("%l64d",&a);
            Insert(a,0);
        }
        printf("Case #%d:\n",ca++);
        while(m--)
        {
            ll a;
            scanf("%I64d",&a);
            printf("%I64d\n",Ques(a,0)^a);
        }
    }
    return 0;
}
View Code

 

字典树

原文:http://www.cnblogs.com/cherryMJY/p/6696298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!