zoukankan      html  css  js  c++  java
  • 字典树

    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
    ");
    }
    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
    ",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:
    ",ca++);
            while(m--)
            {
                ll a;
                scanf("%I64d",&a);
                printf("%I64d
    ",Ques(a,0)^a);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    迅为i.MX6ULL终结者Linux MISC驱动运行测试
    迅为IMX6Q开发板非设备树uboot-修改默认环境变量
    Android固件编译-迅为3399开发板Android8系统编译
    迅为4412开发板-实验LEDS驱动一
    迅为i.MX6ULL终结者设备树下的Platform驱动运行测试
    迅为i.MX6ULL终结者设备树下的Platform驱动实验程序编写
    项目实战-广域网智能家居-把mosquitto移植到arm上
    迅为-iMX6ULL开发板-设置yocto文件系统开机自启动
    muduo库中的核心:std::bind和std::function
    浅析muduo库中的定时器设施
  • 原文地址:https://www.cnblogs.com/cherryMJY/p/6696298.html
Copyright © 2011-2022 走看看