Esempio n. 1
0
        public static int MaxSubarrayXOR_b(int[] arr)
        {
            int result = 0;

            if (arr != null && arr.Length > 0)
            {
                MaxSubarrayXORTrie root = new MaxSubarrayXORTrie();
                Add(root, 0);

                int pre_xor = 0;

                for (int i = 0; i < arr.Length; i++)
                {
                    // update current prefix xor and insert it into Trie
                    pre_xor = pre_xor ^ arr[i];

                    Add(root, pre_xor);

                    // Query for current prefix xor in Trie and update result if required
                    result = Math.Max(result, Search(root, pre_xor));
                }
            }

            return(result);
        }
Esempio n. 2
0
        private static void Add(MaxSubarrayXORTrie root, int pre_xor)
        {
            MaxSubarrayXORTrie temp = root;

            for (int i = sizeof(Int32) - 1; i >= 0; i--)
            {
                int val = pre_xor & (1 << i);

                if (temp.nodes[val] == null)
                {
                    temp.nodes[val] = new MaxSubarrayXORTrie();
                }

                temp = temp.nodes[val];
            }
        }
Esempio n. 3
0
        private static int Search(MaxSubarrayXORTrie root, int pre_xor)
        {
            MaxSubarrayXORTrie temp = root;

            for (int i = sizeof(Int32) - 1; i >= 0; i--)
            {
                // Find current bit in given prefix
                int val = pre_xor & (1 << i);

                // Traverse Trie, first look for a prefix that has opposite bit
                if (temp.nodes[1 - val] != null)
                {
                    temp = temp.nodes[1 - val];
                }
                else if (temp.nodes[val] != null)
                {
                    // If there is no prefix with opposite bit, then look for same bit.
                    temp = temp.nodes[val];
                }
            }

            return(pre_xor ^ (temp.value));
        }