📌  相关文章
📜  将所有出现的 X 替换为 Y 后查找数组异或的查询

📅  最后修改于: 2021-09-03 13:37:33             🧑  作者: Mango

给定一个由N 个不同整数组成的数组arr[]和类型为{X, Y} 的查询Q[][] ,每个查询的任务是在将X替换为Y之后找到所有数组元素的按位异或大批。

例子:

方法:
方法是使用按位异或属性:

  • 假设,有三个元件A,B,X,和它们的异或是,total_xor = A ^ B ^ X。
  • 要从total_xor 中移除X的贡献,请执行total_xor ^ X 。从A ^ B ^ X ^ X = A ^ B (元素与自身的异或为0)这一事实可以验证。
  • 要在total_xor 中添加Y的贡献,只需执行total_xor ^ Y

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Stores the bitwise XOR
// of array elements
int total_xor;
 
// Function to find the total xor
void initialize_xor(int arr[], int n)
{
    // Loop to find the xor
    // of all the elements
    for (int i = 0; i < n; i++) {
        total_xor = total_xor ^ arr[i];
    }
}
 
// Function to find the XOR
// after replacing all occurrences
// of X by Y for Q queries
void find_xor(int X, int Y)
{
    // Remove contribution of
    // X from total_xor
    total_xor = total_xor ^ X;
 
    // Adding contribution of
    // Y to total_xor
    total_xor = total_xor ^ Y;
 
    // Print total_xor
    cout << total_xor << "\n";
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    initialize_xor(arr, n);
 
    vector > Q = { { 5, 6 }, { 8, 1 } };
 
    for (int i = 0; i < Q.size(); i++) {
        find_xor(Q[i][0], Q[i][1]);
    }
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Stores the bitwise XOR
// of array elements
static int total_xor;
 
// Function to find the total xor
static void initialize_xor(int arr[],
                           int n)
{
    // Loop to find the xor
    // of all the elements
    for (int i = 0; i < n; i++)
    {
        total_xor = total_xor ^ arr[i];
    }
}
 
// Function to find the XOR
// after replacing all occurrences
// of X by Y for Q queries
static void find_xor(int X, int Y)
{
    // Remove contribution of
    // X from total_xor
    total_xor = total_xor ^ X;
 
    // Adding contribution of
    // Y to total_xor
    total_xor = total_xor ^ Y;
 
    // Print total_xor
    System.out.print(total_xor + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, 7, 8, 9 };
    int n = arr.length;
 
    initialize_xor(arr, n);
 
    int [][]Q = { { 5, 6 }, { 8, 1 } };
 
    for (int i = 0; i < Q.length; i++)
    {
        find_xor(Q[i][0], Q[i][1]);
    }
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to implement
# the above approach
 
# Stores the bitwise XOR
# of array elements
global total_xor
total_xor = 0
 
# Function to find the total xor
def initialize_xor(arr, n):
 
    global total_xor
 
    # Loop to find the xor
    # of all the elements
    for i in range(n):
        total_xor = total_xor ^ arr[i]
 
# Function to find the XOR
# after replacing all occurrences
# of X by Y for Q queries
def find_xor(X, Y):
     
    global total_xor
 
    # Remove contribution of
    # X from total_xor
    total_xor = total_xor ^ X
 
    # Adding contribution of
    # Y to total_xor
    total_xor = total_xor ^ Y
 
    # Print total_xor
    print(total_xor)
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 5, 7, 8, 9 ]
    n = len(arr)
 
    initialize_xor(arr, n)
 
    Q = [ [ 5, 6 ], [ 8, 1 ] ]
 
    # Function call
    for i in range(len(Q)):
        find_xor(Q[i][0], Q[i][1])
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Stores the bitwise XOR
// of array elements
static int total_xor;
 
// Function to find the total xor
static void initialize_xor(int []arr,
                           int n)
{
     
    // Loop to find the xor
    // of all the elements
    for(int i = 0; i < n; i++)
    {
        total_xor = total_xor ^ arr[i];
    }
}
 
// Function to find the XOR
// after replacing all occurrences
// of X by Y for Q queries
static void find_xor(int X, int Y)
{
     
    // Remove contribution of
    // X from total_xor
    total_xor = total_xor ^ X;
 
    // Adding contribution of
    // Y to total_xor
    total_xor = total_xor ^ Y;
 
    // Print total_xor
    Console.Write(total_xor + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 5, 7, 8, 9 };
    int n = arr.Length;
 
    initialize_xor(arr, n);
 
    int [,]Q = { { 5, 6 }, { 8, 1 } };
 
    for(int i = 0; i < Q.GetLength(0); i++)
    {
        find_xor(Q[i, 0], Q[i, 1]);
    }
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
0
9

时间复杂度: O(N + sizeof(Q))
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live