📌  相关文章
📜  从二进制字符串中删除一个元素以使 XOR 变为零的方法

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

从二进制字符串中删除一个元素以使 XOR 变为零的方法

给定一个二进制字符串,任务是删除数组中的一个整数,使剩余数字的异或为零。任务是计算删除一个元素的方法数,以使该字符串的 XOR 变为零。
例子 :

Input : 10000
Output : 1
We only have 1 ways to 

Input : 10011
Output : 3
There are 3 ways to make XOR 0. We
can remove any of the three 1's.

Input : 100011100
Output : 5
There are 5 ways to make XOR 0. We
can remove any of the give 0's

一个简单的解决方案是一个一个地删除一个元素,然后计算剩余字符串的异或。并计算删除元素使 XOR 0 的出现次数。
一个有效的解决方案基于以下事实。如果 1 的计数是奇数,那么我们必须删除一个 1 以使计数为 0,并且我们可以删除任何一个 1。如果 1 的计数是偶数,则 XOR 为 0,我们可以删除任何 0,XOR 将保持为 0。

C++
// C++ program to count number of ways to
// remove an element so that XOR of remaining
// string becomes 0.
#include 
using namespace std;
 
// Return number of ways in which XOR become ZERO
// by remove 1 element
int xorZero(string str)
{
    int one_count = 0, zero_count = 0;
    int n = str.length();
 
    // Counting number of 0 and 1
    for (int i = 0; i < n; i++)
        if (str[i] == '1')
            one_count++;
        else
            zero_count++;
     
    // If count of ones is even
    // then return count of zero
    // else count of one
    if (one_count % 2 == 0)
        return zero_count;
    return one_count;
}
 
// Driver Code
int main()
{
    string str = "11111";
    cout << xorZero(str) << endl;
    return 0;
}


Java
// Java program to count number of ways to
// remove an element so that XOR of remaining
// string becomes 0.
import java.util.*;
  
class CountWays
{
    // Returns number of ways in which XOR become
    // ZERO by remove 1 element
    static int xorZero(String s)
    {
        int one_count = 0, zero_count = 0;
        char[] str=s.toCharArray();
        int n = str.length;
      
        // Counting number of 0 and 1
        for (int i = 0; i < n; i++)
            if (str[i] == '1')
                one_count++;
            else
                zero_count++;
          
        // If count of ones is even
        // then return count of zero
        // else count of one
        if (one_count % 2 == 0)
            return zero_count;
        return one_count;
    }
 
    // Driver Code to test above function
    public static void main(String[] args)
    {
        String s = "11111";
        System.out.println(xorZero(s)); 
    }
}
 
// This code is contributed by Mr. Somesh Awasthi


Python3
# Python 3 program to count number of
# ways to remove an element so that
# XOR of remaining string becomes 0.
 
# Return number of ways in which XOR
# become ZERO by remove 1 element
def xorZero(str):
    one_count = 0
    zero_count = 0
    n = len(str)
 
    # Counting number of 0 and 1
    for i in range(0, n, 1):
        if (str[i] == '1'):
            one_count += 1
        else:
            zero_count += 1
     
    # If count of ones is even
    # then return count of zero
    # else count of one
    if (one_count % 2 == 0):
        return zero_count
    return one_count
 
# Driver Code
if __name__ == '__main__':
    str = "11111"
    print(xorZero(str))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to count number
// of ways to remove an element
// so that XOR of remaining
// string becomes 0.
using System;
 
class GFG
{
    // Returns number of ways
    // in which XOR become
    // ZERO by remove 1 element
    static int xorZero(string s)
    {
        int one_count = 0,
            zero_count = 0;
         
        int n = s.Length;
     
        // Counting number of 0 and 1
        for (int i = 0; i < n; i++)
            if (s[i] == '1')
                one_count++;
            else
                zero_count++;
         
        // If count of ones is even
        // then return count of zero
        // else count of one
        if (one_count % 2 == 0)
            return zero_count;
        return one_count;
    }
 
    // Driver Code
    public static void Main()
    {
        string s = "11111";
        Console.WriteLine(xorZero(s));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

5