给定二进制字符串的所有子字符串的异或
给定一个大小为N的二进制字符串str ,任务是计算 str 的所有子字符串的按位异或。
例子:
Input: str = “11”
Output: 11
Explanation: The substrings of “11” are: 1, 1, and 11.
Their XOR = 1 ⊕ 1 ⊕ 11 = 11
Input: str = “110”
Output: 111
Explanation: The substrings of 110 are: 1, 1, 0, 11, 10, 110.
Their XOR = 1 ⊕ 1 ⊕ 0 ⊕ 11 ⊕ 10 ⊕ 110 = 111
Input: str = “10101”
Output: 11001
Explanation: The substrings of 10101 are: 1, 10, 101, 1010, 10101, 0, 01, 010, 0101, 1, 10, 101, 0, 01, and 1.
Their XOR = 1 ⊕ 10 ⊕ 101 ⊕ 1010 ⊕ 10101 ⊕ 0 ⊕ 01 ⊕ 010 ⊕ 0101 ⊕ 1 ⊕ 10 ⊕ 101 ⊕ 0 ⊕ 01 ⊕ 1 = 11001
方法:这个问题可以根据以下观察来解决:
XOR of odd number of 1s is always 1. Otherwise, the XOR is 0.
Each jth bit can be the ith bit in a substring when 0 ≤ j ≤ N-i.
So each character has contribution for the last bit (LSB) of the result.
All characters from i = 0 to N-2 has contribution for 2nd last bit and so on.
按照下面提到的步骤来利用上述观察来解决这个问题:
- 创建一个数组(比如发生[])来存储对从 LSB 结束的第 i 个索引有贡献的 1 的总数到结果 XOR 中。
- 现在从 LSB 端迭代(迭代器i ):
- 如果第 i 个索引处 1 的总数为奇数,则结果 XOR 将从 LSB 端开始的第 i位为1 。
- 否则,第 i位的值为 0。
- 返回结果异或。
下面是上述方法的实现:
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to find the XOR
string totalXOR(string s, int n)
{
// Vector for storing the occurrances
vector occurrance;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
occurrance.push_back(i + 1);
else
occurrance.push_back(0);
}
// Calculating the total occurrences
// of nth bit
int sums
= accumulate(occurrance.begin(),
occurrance.end(), 0);
string ans = "";
for (int i = 0; i < n; i++) {
// Checking if the total occurrances
// are odd
if (sums % 2 == 1)
ans = "1" + ans;
else
ans = "0" + ans;
sums -= occurrance.back();
occurrance.pop_back();
}
return ans;
}
// Driver Code
int main()
{
int N = 5;
string str = "10101";
cout << totalXOR(str, N);
return 0;
}
Java
// Java program to implement the approach
import java.io.*;
import java.util.ArrayList;
public class GFG {
// function to find XOR
static String totalXOR(String s, int n)
{
// initializing occurrance list
ArrayList occurrance
= new ArrayList();
for (int i = 0; i < n; i++) {
if ((s.charAt(i)) == '1')
occurrance.add(i + 1);
else
occurrance.add(0);
}
// calculating sum of occurrance
int sums = 0;
for (int i : occurrance)
sums += i;
String ans = "";
for (int i = 0; i < n; i++) {
// Checking if the total occurrances
// are odd
if (sums % 2 == 1)
ans = "1" + ans;
else
ans = "0" + ans;
// subtracting last element of occurrance from
// sums
sums -= occurrance.get(occurrance.size() - 1);
// removing last element of occurrance
occurrance.remove(occurrance.size() - 1);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
String str = "10101";
System.out.print(totalXOR(str, N));
}
}
// This code is contributed by phasing17
Python3
# Python Program to implement
# above approach
def totalXOR(string, n):
# Storing the occurrances of 1s
occurrances = [i + 1 if string[i] ==
'1' else 0 for i in range(n)]
# Calculating the occurrances for nth bit
sums = sum(occurrances)
ans = ''
for i in range(n):
ans \
= ['0', '1'][sums % 2 == 1] + ans
# Substracting the occurrances of
# (i + 1)th bit from ith bit
sums -= occurrances[-1]
del occurrances[-1]
return ans
# Driver Code
if __name__ == '__main__':
N = 5
str = "10101"
print(totalXOR(str, N))
Javascript
11001
时间复杂度: O(N)
辅助空间: O(N)