📌  相关文章
📜  将给定二进制字符串转换为全1所需的最小给定操作

📅  最后修改于: 2021-05-08 18:16:14             🧑  作者: Mango

给定一个二进制数作为长度为L的字符串str 。我们的任务是找到所需的最小操作数,以使该数目变为2 L -1 ,这是一个仅由长度L的1组成的字符串。
在每个操作中,数字N可以替换为N xor(N +1)

例子:

方法:执行给定的操作后,可以观察到,为了获得所需的数量,最后,操作数量将为:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the number
// of operations required
int changeToOnes(string str)
{
  
    // ctr will store the number of
    // consecutive ones at the end
    // of the given binary string
    int i, l, ctr = 0;
    l = str.length();
  
    // Loop to find number of 1s
    // at the end of the string
    for (i = l - 1; i >= 0; i--) {
  
        // If the current character is 1
        if (str[i] == '1')
            ctr++;
  
        // If we encounter the first 0
        // from the LSB position then
        // we'll break the loop
        else
            break;
    }
  
    // Number of operations
    // required is (l - ctr)
    return l - ctr;
}
  
// Function to remove leading
// zeroes from the string
string removeZeroesFromFront(string str)
{
    string s;
  
    int i = 0;
  
    // Loop until s[i] becomes
    // not equal to 1
    while (i < str.length() && str[i] == '0')
        i++;
  
    // If we reach the end of
    // the string, it means that
    // string contains only 0's
    if (i == str.length())
        s = "0";
  
    // Return the string without
    // leading zeros
    else
        s = str.substr(i, str.length() - i);
    return s;
}
  
// Driver code
int main()
{
    string str = "10010111";
  
    // Removing the leading zeroes
    str = removeZeroesFromFront(str);
  
    cout << changeToOnes(str);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the number
// of operations required
static int changeToOnes(String str)
{
  
    // ctr will store the number of
    // consecutive ones at the end
    // of the given binary string
    int i, l, ctr = 0;
    l = str.length();
  
    // Loop to find number of 1s
    // at the end of the string
    for (i = l - 1; i >= 0; i--) 
    {
  
        // If the current character is 1
        if (str.charAt(i) == '1')
            ctr++;
  
        // If we encounter the first 0
        // from the LSB position then
        // we'll break the loop
        else
            break;
    }
  
    // Number of operations
    // required is (l - ctr)
    return l - ctr;
}
  
// Function to remove leading
// zeroes from the string
static String removeZeroesFromFront(String str)
{
    String s;
  
    int i = 0;
  
    // Loop until s[i] becomes
    // not equal to 1
    while (i < str.length() &&
               str.charAt(i) == '0')
        i++;
  
    // If we reach the end of
    // the string, it means that
    // string contains only 0's
    if (i == str.length())
        s = "0";
  
    // Return the string without
    // leading zeros
    else
        s = str.substring(i, str.length() - i);
    return s;
}
  
// Driver code
public static void main(String[] args) 
{
    String str = "10010111";
  
    // Removing the leading zeroes
    str = removeZeroesFromFront(str);
  
    System.out.println(changeToOnes(str));
}
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to return the number 
# of operations required 
def changeToOnes(string) : 
  
    # ctr will store the number of 
    # consecutive ones at the end 
    # of the given binary string 
    ctr = 0; 
    l = len(string); 
  
    # Loop to find number of 1s 
    # at the end of the string 
    for i in range(l - 1, -1, -1) : 
  
        # If the current character is 1 
        if (string[i] == '1') :
            ctr += 1; 
  
        # If we encounter the first 0 
        # from the LSB position then 
        # we'll break the loop 
        else :
            break; 
  
    # Number of operations 
    # required is (l - ctr) 
    return l - ctr; 
  
# Function to remove leading 
# zeroes from the string 
def removeZeroesFromFront(string) : 
  
    s = ""; 
  
    i = 0; 
  
    # Loop until s[i] becomes 
    # not equal to 1 
    while (i < len(string) and 
                   string[i] == '0') :
        i += 1; 
  
    # If we reach the end of 
    # the string, it means that 
    # string contains only 0's 
    if (i == len(string)) :
        s = "0"; 
  
    # Return the string without 
    # leading zeros 
    else :
        s = string[i: len(string) - i]; 
          
    return s; 
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "10010111"; 
  
    # Removing the leading zeroes 
    string = removeZeroesFromFront(string); 
  
    print(changeToOnes(string)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the number
// of operations required
static int changeToOnes(String str)
{
  
    // ctr will store the number of
    // consecutive ones at the end
    // of the given binary string
    int i, l, ctr = 0;
    l = str.Length;
  
    // Loop to find number of 1s
    // at the end of the string
    for (i = l - 1; i >= 0; i--) 
    {
  
        // If the current character is 1
        if (str[i] == '1')
            ctr++;
  
        // If we encounter the first 0
        // from the LSB position then
        // we'll break the loop
        else
            break;
    }
  
    // Number of operations
    // required is (l - ctr)
    return l - ctr;
}
  
// Function to remove leading
// zeroes from the string
static String removeZeroesFromFront(String str)
{
    String s;
  
    int i = 0;
  
    // Loop until s[i] becomes
    // not equal to 1
    while (i < str.Length &&
               str[i] == '0')
        i++;
  
    // If we reach the end of
    // the string, it means that
    // string contains only 0's
    if (i == str.Length)
        s = "0";
  
    // Return the string without
    // leading zeros
    else
        s = str.Substring(i, str.Length - i);
    return s;
}
  
// Driver code
public static void Main(String[] args) 
{
    String str = "10010111";
  
    // Removing the leading zeroes
    str = removeZeroesFromFront(str);
  
    Console.WriteLine(changeToOnes(str));
}
}
  
// This code is contributed by 29AjayKumar


输出:
5

时间复杂度: O(n)