📌  相关文章
📜  通过反转或翻转子字符串的字符来最小化使二进制字符串的所有字符等于“1”的成本

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

通过反转或翻转子字符串的字符来最小化使二进制字符串的所有字符等于“1”的成本

给定一个二进制字符串S和两个整数A ,表示反转子字符串的成本,和B ,表示翻转子字符串的所有字符的成本,任务是找到将字符串S减少到1最小成本仅限s。

例子:

方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 0值初始化一个变量P以存储不相交的连续0组的计数。
  • 另外,初始化一个变量 say count0以存储组中0的数量的临时计数。
  • 遍历字符串S的字符并执行以下操作:
    • 如果当前字符为“ 0 ”,则将计数增加1
    • 否则,如果计数大于0 ,则将P增加1 ,然后将0分配给count
  • 如果计数大于0 ,则将P1。
  • 将获得的最小成本打印为(P-1)*A+B。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate minimum cost to
// convert all the characters of S to '1'
void MinimumCost(string S, int A, int B)
{
    // Stores the result
    int count = 0;
 
    // Stores the number of groups
    // that have 0 as characters
    int group = 0;
 
    // Traverse the string S
    for (int i = 0; i < S.size(); i++) {
 
        // If current character is '0'
        if (S[i] == '0') {
            count += 1;
        }
        else {
            // If count is greater
            // than 0
            if (count > 0) {
                group += 1;
            }
 
            // Set the count to 0
            count = 0;
        }
    }
 
    // If the last few consecutive
    // characters are '0'
    if (count > 0)
        group += 1;
 
    // If string contains
    // all characters as '1'
    if (group == 0) {
        cout << 0 << endl;
    }
    else {
        // Minimum Cost
        cout << min(A, B) * (group - 1) + B;
    }
}
 
// Driver Code
int main()
{
 
    // Given Input
    int A = 1;
    int B = 5;
    string S = "01100";
 
    // Function Call
    MinimumCost(S, A, B);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(String S, int A, int B)
{
     
    // Stores the result
    int count = 0;
 
    // Stores the number of groups
    // that have 0 as characters
    int group = 0;
 
    // Traverse the string S
    for(int i = 0; i < S.length(); i++)
    {
         
        // If current character is '0'
        if (S.charAt(i) == '0')
        {
            count += 1;
        }
        else
        {
             
            // If count is greater
            // than 0
            if (count > 0)
            {
                group += 1;
            }
             
            // Set the count to 0
            count = 0;
        }
    }
 
    // If the last few consecutive
    // characters are '0'
    if (count > 0)
        group += 1;
 
    // If string contains
    // all characters as '1'
    if (group == 0)
    {
        System.out.println(0);
    }
    else
    {
         
        // Minimum Cost
        System.out.print(Math.min(A, B) *
                        (group - 1) + B);
    }
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given Input
    int A = 1;
    int B = 5;
    String S = "01100";
     
    // Function Call
    MinimumCost(S, A, B);
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Function to calculate minimum cost to
# convert all the characters of S to '1'
def MinimumCost(S, A, B):
    # Stores the result
    count = 0
 
    # Stores the number of groups
    # that have 0 as characters
    group = 0
 
    # Traverse the S
    for i in range(len(S)):
        # If current character is '0'
        if (S[i] == '0'):
            count += 1
        else:
            # If count is greater
            # than 0
            if (count > 0):
                group += 1
            # Set the count to 0
            count = 0
 
    # If the last few consecutive
    # characters are '0'
    if (count > 0):
        group += 1
 
    # If contains
    # all characters as '1'
    if (group == 0):
        print(0)
    else:
        # Minimum Cost
        print(min(A, B) * (group - 1) + B)
 
# Driver Code
if __name__ == '__main__':
 
    # Given Input
    A = 1
    B = 5
    S = "01100"
 
    # Function Call
    MinimumCost(S, A, B)
 
     # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(string S, int A, int B)
{
     
    // Stores the result
    int count = 0;
 
    // Stores the number of groups
    // that have 0 as characters
    int group = 0;
 
    // Traverse the string S
    for(int i = 0; i < S.Length; i++)
    {
         
        // If current character is '0'
        if (S[i] == '0')
        {
            count += 1;
        }
        else
        {
             
            // If count is greater
            // than 0
            if (count > 0)
            {
                group += 1;
            }
 
            // Set the count to 0
            count = 0;
        }
    }
 
    // If the last few consecutive
    // characters are '0'
    if (count > 0)
        group += 1;
 
    // If string contains
    // all characters as '1'
    if (group == 0)
    {
        Console.WriteLine(0);
    }
    else
    {
         
        // Minimum Cost
        Console.Write(Math.Min(A, B) *
                      (group - 1) + B);
    }
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    int A = 1;
    int B = 5;
    string S = "01100";
 
    // Function Call
    MinimumCost(S, A, B);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
6

时间复杂度: O(N)
辅助空间: O(1)