zoukankan      html  css  js  c++  java
  • csu 10月 月赛 F 题 ZZY and his little friends

    一道字典树异或的题,但是数据比较水,被大家用暴力给干掉了!

    以前写过一个类似的题,叫做the longest xor in tree;

    两个差不多吧!

    好久没写字典树了,复习一下!

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 100010
     5 using namespace std;
     6 int n,v[maxn],node,next[maxn][2],end[maxn];
     7 
     8 void add(int cur,int k)
     9 {
    10     memset(next[node],0,sizeof(next[node]));
    11     end[node]=0;
    12     next[cur][k]=node++;
    13 }
    14 
    15 int cal(int x)
    16 {
    17     int i,k,cur=0;
    18     for(i=30;i>=0;i--)
    19     {
    20         k=((1<<i)&x)?0:1;
    21         if(next[cur][k]) cur=next[cur][k];
    22         else cur=next[cur][1-k];
    23     }
    24     return (x^end[cur]);
    25 }
    26 
    27 int main()
    28 {
    29     int k,x,cur,ans,cp;
    30     while(!scanf("%d%d",&n,&cp)!=EOF)
    31     {
    32         node=1,ans=0;
    33         memset(next[0],0,sizeof(next[0]));
    34         for(int i=0;i<n;i++)
    35         {
    36             scanf("%d",&x);
    37             v[i]=x;
    38             cur=0;
    39             for(int j=30;j>=0;j--)
    40             {
    41                 k=((1<<j)&x)?1:0;
    42                 if(next[cur][k]==0) add(cur,k);
    43                 cur=next[cur][k];
    44             }
    45             end[cur]=x;
    46         }
    47         for(int i=0;i<n;i++)ans=max(ans,cal(v[i]));
    48         if(ans>cp)puts("YES");
    49         else puts("NO");
    50     }
    51     return 0;
    52 }
    View Code

    另一种写法:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define maxn 100005
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     node *p[2];
    10 } no[maxn];
    11 int w[30],nocount;
    12 
    13 node *newnode()
    14 {
    15     node *v=no+nocount++;
    16     for(int i=0;i<2;i++)v->p[i]=NULL;
    17     return v;
    18 }
    19 
    20 void insert()
    21 {
    22     node *nono=no;
    23     for(int i=29;i>=0;i--)
    24     {
    25         if(nono->p[w[i]]==NULL)
    26             nono->p[w[i]]=newnode();
    27             nono=nono->p[w[i]];
    28     }
    29 }
    30 
    31 int cal()
    32 {
    33     int ans=0;
    34     node *nono=no;
    35     for(int i=29;i>=0;i--)
    36     {
    37         if(nono->p[1-w[i]]!=NULL){ans+=(1<<i);nono=nono->p[1-w[i]];}
    38         else if(nono->p[w[i]]!=NULL){nono=nono->p[w[i]];}
    39     }
    40     return ans;
    41 }
    42 
    43 int main()
    44 {
    45     int n,m,x;
    46     while(scanf("%d%d",&n,&m)!=EOF)
    47     {
    48         memset(no,0,sizeof no);
    49         nocount=1;
    50         int mm=0;
    51         for(int i=0; i<n; i++)
    52         {
    53             scanf("%d",&x);
    54             for(int i=0;i<30;i++){w[i]=x%2,x>>=1;}
    55             if(i!=0)mm=max(mm,cal());
    56             insert();
    57         }
    58         if(mm>m)puts("YES");
    59         else puts("NO");
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    OC学习一周总结
    C语言基础学习总结
    123
    汇编中中括号[]作用以及lea和mov指令的区别
    C#获取局域网内所有的SQL Server服务器名
    .net 初中级程序员招聘
    C#在客户端与 JS 交互
    [ZT]Mac下安装mysql和workbench
    Eclipse文件夹导入Jar
    Tomcat配置后提示404的解决办法
  • 原文地址:https://www.cnblogs.com/yours1103/p/3349782.html
Copyright © 2011-2022 走看看