public int BruteForce(int one, int two) { int maxXor = 0; while (one < two) { int oneTemp = one + 1; while (oneTemp <= two) { int curXor = one ^ oneTemp; if (maxXor < curXor) maxXor = curXor; oneTemp++; } one++; } return maxXor; }
using System; namespace MaxXor { public class Trie { //for integer representation in binary system 2^32 public static readonly int MaxLengthOfBits = 32; //size of alphabet public static readonly int N = 2; class Node { public Node[] next = new Node[Trie.N]; } private Node _root; } }
private bool[] ConvertDecimalToBInary(int number) { int counter = Trie.MaxLengthOfBits; bool[] result = new bool[counter]; while (number > 0) { result[--counter] = Convert.ToBoolean(number % 2); number /= 2; } return result; }
private int ConvertBinaryToDecimal(bool[] bits) { int result = 0; int base_val = 1; for (int i = bits.Length - 1; i >= 0; i--) { result += Convert.ToInt32(bits[i]) * base_val; base_val *= 2; } return result; }
public void AddValue(bool[] binaryNumber) { _root = AddValue(_root, binaryNumber, 0); } private Node AddValue(Node node, bool[] val, int d) { if (node == null) node = new Node(); //if least sagnificient bit has been added //need return if (d == val.Length) { return node; } // get 0 or 1 index of next array(length 2) int index = Convert.ToInt32(val[d]); node.next[index] = AddValue(node.next[index], val, ++d); return node; }
it is enough to find the very first bit, which is different, nothing can be done with the bits above;So, we find the very first bit, which is different, for this we will move from the root of the links until we meet a node that has both links. Then all the bits below are made in units. As a result, we get the result.
public bool[] GetMaxXor() { bool[] result = new bool[Trie.MaxLengthOfBits]; Node cur = _root; //find index the most significant bit, which is different int index; for (index = 0; index < Trie.MaxLengthOfBits; index++) { //are bits differ? if (cur.next[0] != null && cur.next[1] != null) { //the most significant bit, which is different result[index] = true; break; } //descent down the tree cur = cur.next[0] ?? cur.next[1]; } //all the bits below do 1 while (index < Trie.MaxLengthOfBits) { result[index] = true; index++; } return result; }
public bool[] GetMaxXor() { bool[] result = new bool[Trie.MaxLengthOfBits]; Node oneNode = _root, twoNode = _root; //for each bit from most significant bit to least significant bit for (int i = 0; i < Trie.MaxLengthOfBits; i++) { //getting current bit result[i] = GetBit(oneNode, twoNode); //go to next nodes UpdateNodes(ref oneNode, ref twoNode); } return result; } //we need update nodes after each iteration //we can stay on single node or split on two nodes private void UpdateNodes(ref Node one, ref Node two) { if (one.Equals(two)) { if (one.next[1] != null && one.next[0] != null) { two = one.next[1]; one = one.next[0]; } else { one = two = ((one.next[1] != null) ? one.next[1] : one.next[0]); } } else { if (one.next[1] != null && two.next[0] != null) { one = one.next[1]; two = two.next[0]; } else if (one.next[0] != null && one.next[1] == null) { one = one.next[0]; two = two.next[1]; } else { one = one.next[1] ?? one.next[0]; two = two.next[1] ?? two.next[0]; } } } //if it's possible, we will try to get one. private bool GetBit(Node one, Node two) { if (one.Equals(two)) { // 0 xor 1 == 1; 1 xor 0 == 1 if (one.next[0] != null && one.next[1] != null) return true; // 0 xor 0 == 0; 1 xor 1 == 0 else return false; } else { if ((one.next[1] != null && two.next[0] != null) || // 1 xor 0 == 1 (one.next[0] != null && one.next[1] == null)) // 0 xor 1 == 1 { return true; } else {// 0 xor 0 == 0; 1 xor 1 == 0 return false; } } }
Source: https://habr.com/ru/post/245801/
All Articles