zoukankan      html  css  js  c++  java
  • TOJ 3176 Challenge from XOR

    Description

    Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator that returns one value if one, but not both, of its operands is one. It is also called Exclusive-OR. Now Mr. AngelClover wonders if it is possible to pick up some numbers from a certain number set, such that the result of Exclusive-OR is zero. And he wants the numbers to be as few as possible but there should be at least one.
    Can you help him?

    Input

    The first line is a positive integer N, which means the number of the number set. And there are N lines followed, in each there is a single positive integer, which is the number in the set. Each number in the set is an integer between 0 and 2^16 inclusive. 0< N <=30. (Please use standard input, and don’t read or write files.)

    Output

    There is only one line in the output, which is the smallest number as described above. If Mr. AngelClover can NOT find such numbers after XOR is zero, output -1. (Please use standard output, and don’t read or write files.)

    Sample Input

    4
    1
    2
    3
    4
    

    Sample Output

    3

    Hint

    Numbers which are picked up are 1, 2, 3.

    For 40% input data cases, N <= 15.

    Source

    NKPC5

    题目意思就是选几个数进行异或运算结果为0的最小个数 。一开始用觉得数据不大,用深搜果断超时。

    后来想想觉得应该让个数最小深搜会产生大量没用的结果而且要全部遍历,改用广搜。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <queue>
     4 using namespace std;
     5 
     6 int n;
     7 int a[40];
     8 int ans;
     9 
    10 struct Node{
    11     int val;
    12     int step;
    13     int bs;
    14 };
    15 
    16 int bfs(){
    17     queue<Node> Q;
    18     Node n1;
    19     n1.val=0;
    20     n1.step=0;
    21     n1.bs=0;
    22     Q.push(n1);
    23     while(!Q.empty()){        
    24         Node now=Q.front();
    25         Q.pop();
    26         for(int i=now.bs; i<n; i++){
    27             int  v=a[i]^now.val;
    28             if(v==0)return now.step+1;
    29             else{
    30                 Node n2;
    31                 n2.val=v;
    32                 n2.step=now.step+1;
    33                 n2.bs=i+1;
    34                 Q.push(n2);
    35             }
    36         }
    37     }
    38     return -1;
    39 }
    40 
    41 int main(int argc, char *argv[])
    42 {
    43     while( scanf("%d",&n)!=EOF ){
    44         for(int i=0; i<n; i++){
    45             scanf("%d",&a[i]);
    46         }
    47         ans=bfs();
    48         printf("%d
    ",ans);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    ntohs, ntohl, htons,htonl的比较和详解
    转 linux socket的select函数例子
    转 结构体中字节对齐问题(转载)
    C语言中volatile关键字的作用
    转 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法
    转 常见hash算法的原理
    转 从头到尾彻底解析Hash表算法
    [CEOI2020 D2T1 / CF1403A] 权力药水The Potion of Great Power 题解
    [CEOI2020 D1T3 / CF1402C] 星际迷航Star Trek 题解
    [CEOI2020 D1T2 / CF1402B] 道路Roads 题解
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3567915.html
Copyright © 2011-2022 走看看