📌  相关文章
📜  给定二进制字符串的所有子字符串的异或

📅  最后修改于: 2022-05-13 01:56:05.026000             🧑  作者: Mango

给定二进制字符串的所有子字符串的异或

给定一个大小为N的二进制字符串str ,任务是计算 str 的所有子字符串的按位异或。

例子:

方法:这个问题可以根据以下观察来解决:

按照下面提到的步骤来利用上述观察来解决这个问题:

  • 创建一个数组(比如发生[])来存储对从 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)