📌  相关文章
📜  计算最小位以翻转,以使A和B的XOR等于C

📅  最后修改于: 2021-04-27 20:49:55             🧑  作者: Mango

给定一个N位的三个二进制序列A,B和C的序列。计算翻转A和B所需的最小位,以使A和B的XOR等于C。例如

Input: N = 3
       A = 110
       B = 101
       C = 001
Output: 1
We only need to flip the bit of 2nd position
of either A or B, such that A ^ B = C i.e., 
100 ^ 101 = 001

天真的方法是生成A和B中所有可能的比特组合,然后对其进行XOR运算以检查其是否等于C。这种方法的时间复杂度呈指数增长,因此对于较大的N值来说,这不会再好了。
一种有效的方法是使用XOR的概念。

XOR Truth Table
  Input    Output
 X     Y     Z
 0     0  -  0
 0     1  -  1
 1     0  -  1
 1     1  -  0

如果我们概括一下,我们会发现在A和B的任何位置上,我们只需要翻转A或B的i个位置(从0到N-1),否则我们将无法获得最少的位数。
因此,在i的任何位置(0到N-1),您都会遇到两种情况,即A [i] == B [i]或A [i]!= B [i]。让我们一一讨论。

  • 如果A [i] == B [i],那么这些位的XOR将为0,则在C []中会出现两种情况:C [i] == 0或C [i] == 1。
    如果C [i] == 0,则无需翻转位,但如果C [i] == 1,则我们必须翻转A [i]或B [i]中的位,以便1 ^ 0 == 1或0 ^ 1 == 1。
  • 如果A [i]!= B [i],则这些位的XOR值等于1,在C中再次出现两种情况,即C [i] == 0或C [i] == 1。
    因此,如果C [i] == 1,则无需翻转位,但如果C [i] == 0,则需要翻转A [i]或B [i]中的位,以便0 ^ 0 == 0或1 ^ 1 == 0
C++
// C++ code to count the Minimum bits in A and B
#include
using namespace std;
 
int totalFlips(char *A, char *B, char *C, int N)
{
    int count = 0;
    for (int i=0; i < N; ++i)
    {
        // If both A[i] and B[i] are equal
        if (A[i] == B[i] && C[i] == '1')
            ++count;
 
        // If Both A and B are unequal
        else if (A[i] != B[i] && C[i] == '0')
            ++count;
    }
    return count;
}
 
//Driver Code
int main()
{
    //N represent total count of Bits
    int N = 5;
    char a[] = "10100";
    char b[] = "00010";
    char c[] = "10011";
 
    cout << totalFlips(a, b, c, N);
 
    return 0;
}


Java
// Java code to count the Minimum bits in A and B
class GFG {
     
    static int totalFlips(String A, String B,
                                String C, int N)
    {
        int count = 0;
         
        for (int i = 0; i < N; ++i)
        {
            // If both A[i] and B[i] are equal
            if (A.charAt(i) == B.charAt(i) &&
                            C.charAt(i) == '1')
                ++count;
     
            // If Both A and B are unequal
            else if (A.charAt(i) != B.charAt(i)
                          && C.charAt(i) == '0')
                ++count;
        }
         
        return count;
    }
     
    //driver code
    public static void main (String[] args)
    {
        //N represent total count of Bits
        int N = 5;
        String a = "10100";
        String b = "00010";
        String c = "10011";
     
        System.out.print(totalFlips(a, b, c, N));
    }
}
 
// This code is contributed by Anant Agarwal.


Python
# Python code to find minimum bits to be flip
def totalFlips(A, B, C, N):
 
    count = 0
    for i in range(N):
         
        # If both A[i] and B[i] are equal
        if A[i] == B[i] and C[i] == '1':
            count=count+1
 
        # if A[i] and B[i] are unequal
        elif A[i] != B[i] and C[i] == '0':
            count=count+1
    return count
 
 
# Driver Code
# N represent total count of Bits
N = 5
a = "10100"
b = "00010"
c = "10011"
print(totalFlips(a, b, c, N))


C#
// C# code to count the Minimum
// bits flip in A and B
using System;
 
class GFG {
 
    static int totalFlips(string A, string B,
                          string C, int N)
    {
        int count = 0;
        for (int i = 0; i < N; ++i) {
 
            // If both A[i] and B[i] are equal
            if (A[i] == B[i] && C[i] == '1')
                ++count;
 
            // If Both A and B are unequal
            else if (A[i] != B[i] && C[i] == '0')
                ++count;
        }
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        // N represent total count of Bits
        int N = 5;
        string a = "10100";
        string b = "00010";
        string c = "10011";
 
        Console.Write(totalFlips(a, b, c, N));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出:

2