Algorithm-Find out that other occurrences of an even number of arrays appear 1 times, 2 times, 3 times in number

Source: Internet
Author: User
Tags findone

#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <iostream>#include <vector>#include <string>#include <set>#include <unordered_set>#include <queue>#include <map>using namespace STD;//One. Find a number that appears one time/ * The idea is to use the different or, different or the role can be the same bit of the same 0,0 0 this is exactly the same as the idea of even. When all the numbers are different or up, it will be found that only one occurrence of the number above the 1 bit will be retained. */intFindOneintA[],intN) {intAns =0; for(inti =0; I < n;    i++) {ans ^= a[i]; }returnAns;}//Two. Find out two occurrences of a number/* There are two 1-time numbers and 1 methods that don't work, but one way to do this is to divide the entire array into two parts. Each part includes a number so that it can be converted to a method that has a number of occurrences. How to decompose, first need to find out the difference between the two numbers: a:0 0 1 1 b:0 1 0 1 xor: 0 1 1 0 We will find that the bit XOR of A and B, there are 4 cases, the result of which is 1. When the result bit XOR equals 1, A and B bits are definitely different. That's the difference, we can find out whether a bit is a single bit of 2 groups. */intFindbit1 (intN) {//Find out low start first 1 bit, other clear 0    returnn&~ (N-1);//N&-n can also}voidFindtwo (intA[],intN) {intANS1 =0, Ans2 =0;intFlag = FindOne (A, n);//All XOR, result =a^b other becomes 0;Flag = FINDBIT1 (flag); for(inti =0; I < n; i++) {if(A[i] & flag) ans1 ^= a[i];ElseAns2 ^= A[i]; }cout<< ans1 << Endl;cout<< ans2 << Endl;}//Three. Find the number of 3 occurrences 1 times/ * This is more difficult because it cannot be divided into 2 groups directly. For example A,B,CA 0 0 0 0 1 1 1 1b 0 0 1 1 0 0 1 1c 0 1 0 1 0 1 0 1r 0 1 1 0 1 0 0 1 When the result is 1, A,b,c has two possibilities, one is 0 0 1 and the other is 1 1 1. Kind of situation, more tricky online there is a way is no matter 3,721, first press 0 0 1 This situation, and then the ans1 and Ans2 to the comparison of a variety of situations, more complex. The following is the use of the counter-law and the function of the construction process is more complex, if understood, directly on the chatter out, compared to recommend 1. First XOR or all numbers x=a^b^c ..... Other XOR = 0 2. Re-use X XOR all numbers x^a[i]. This makes no difference in the presence of an even number of times, because we do not care about their actual size but x^a, x^b, x^c These three numbers have changed a lot. Our goal is to divide these three numbers into two unique groups. We will find X^a ^ x^b ^ x^c = 0, and three numbers cannot be 0, and they are not the same. (since x^a=b^c,b is not equal to C so b^c is not equal to 0) then do a trick to make N1=f (X^a), N2=f (x^b), N3=f (X^c), Ni=f (X^a[i]) where F's role is to keep low the last 1 others are all 0 (XXXXX1000 into 000001000) and then there are only one bit in the N1,N2,N3 and 1, and now the difference between the n1,n2,n3 problem becomes N1 0 0 0 0 1 1 1 1n2 0 0 1 1 0 0 1 1n3 0 1 0 1 0 1 0 1r 0 1 1 0 1 0 0  1 before is three number possible at the same time for 1, may also have only one for 1, such r=1; But three numbers cannot be at the same time for 1. Otherwise with n1^n2^n3=0 contradiction (because x^a ^ x^b ^ x^c =0 ensured that any one could not be three 1) final: Just P=f (N1^N2^N3) pick P&f (X^a[i])!=0 for a group of ==0 for a group on the line * *voidFindthree (intA[],intN) {intx = FindOne (A, n);intp =0; for(inti =0; I < n;    i++) P ^= findbit1 (X^a[i]); p = findbit1 (p);intANS1 =0, Ans2 =0, ANS3 =0; for(inti =0; I < n; i++) {if(P&findbit1 (X^a[i])) ans1 ^= A[i]; }cout<< ans1 << Endl;//ans1 kicked out     for(inti =0; I < n; i++) {if(ans1 = = A[i]) {Swap (A[i], a[n-1]); Break; }} findtwo (A, N-1);}intMain () {intA1[] = {1,1,2,2,3,4,4};intA2[] = {1,1,2,2,3,4,4,5};intA3[] = {6,1,1,2,2,3,4,4,5};cout<<"Find a"<< Endl;cout<< FindOne (A1,sizeof(A1)/sizeof(int)) <<endl;cout<<"Find Two"<< Endl; Findtwo (A2,sizeof(A2)/sizeof(int));cout<<"Find three"<< Endl; Findthree (A3,sizeof(A3)/sizeof(int));    GetChar (); GetChar ();return 0;}

Algorithm-Find out that other occurrences of an even number of arrays appear 1 times, 2 times, 3 times in number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.