📌  相关文章
📜  通过根据给定条件拆分给定的二进制字符串来最大化总和

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

通过根据给定条件拆分给定的二进制字符串来最大化总和

给定两个长度为N的二进制字符串str1str2 ,任务是以在给定条件下总和最大的方式拆分字符串。

  • 将相同位置的两个字符串拆分为相等长度的子字符串。
  • 如果两个子字符串都只有 0,则要添加的子字符串的值为 1。
  • 如果两个子字符串都只有 1,则要添加的子字符串的值为 0。
  • 如果两个子字符串都有 0 和 1,则要添加的子字符串的值为 2。

返回可能的最大总和。

例子:

方法:这个问题可以使用贪心方法来解决。首先,取一些不同的值对均值 (0, 1) 因为它给出最大值 2 或者与一个不同的值对具有相同的值对,最大值为 2 所以没有任何好处。然后尝试将 (0, 0) 与 (1, 1) 配对,反之亦然,以使总和最大化。

  • 将 MaxSum 初始化为 0。
  • 遍历字符串。如果 Ai 不等于 Bi,则将 MaxSum 增加 2。
  • 再次遍历字符串并尝试与相反的值配对,即 0 与 1 或 1 与 0。
  • 检查字符串的上一个和下一个值是否相反,将 MaxSum 增加 2。
  • 否则,如果该对仅 (0, 0) 将 MaxSum 增加 1。

下面是上述方法的实现:

C++
// C++ program to split two
// binary strings in such a way
// such that sum is maximum.
#include 
using namespace std;
 
// Function to split strings
// to find maximum sum
int MaxSum(string a, string b)
{
    int ans = 0;
    for (int i = 0; i < a.length(); i++) {
        if (a[i] != b[i])
            ans += 2;
    }
    int n = a.length();
 
    // Traverse the strings.
    for (int i = 0; i < n; i++) {
        if (a[i] == b[i]) {
            if (a[i] == '0') {
 
                // If Ai is not equal to Bi,
                // increment the MaxSum by 2
                if (i + 1 < n and a[i + 1] == '1'
                    and b[i + 1] == '1') {
                    ans += 2, i++;
                }
 
                // Else if the pair is only (0, 0)
                // increment MaxSum by 1.
                else {
                    ans += 1;
                }
            }
            else {
                if (i + 1 < n and a[i + 1] == '0'
                    and b[i + 1] == '0') {
                    ans += 2, i++;
                }
                else {
                    ans += 0;
                }
            }
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    string a = "0101000";
    string b = "1101100";
    cout << MaxSum(a, b);
    return 0;
}


Java
// Java program to split two
// binary Strings in such a way
// such that sum is maximum.
class GFG {
 
  // Function to split Strings
  // to find maximum sum
  static int MaxSum(String a, String b) {
    int ans = 0;
    for (int i = 0; i < a.length(); i++) {
      if (a.charAt(i) != b.charAt(i))
        ans += 2;
    }
    int n = a.length();
 
    // Traverse the Strings.
    for (int i = 0; i < n; i++) {
      if (a.charAt(i) == b.charAt(i)) {
        if (a.charAt(i) == '0') {
 
          // If Ai is not equal to Bi,
          // increment the MaxSum by 2
          if (i + 1 < n && a.charAt(i + 1) == '1'
              && b.charAt(i + 1) == '1') {
            ans += 2;
            i++;
          }
 
          // Else if the pair is only (0, 0)
          // increment MaxSum by 1.
          else {
            ans += 1;
          }
        } else {
          if (i + 1 < n && a.charAt(i + 1) == '0'
              && b.charAt(i + 1) == '0') {
            ans += 2;
            i++;
          } else {
            ans += 0;
          }
        }
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void main(String args[]) {
 
    String a = "0101000";
    String b = "1101100";
    System.out.println(MaxSum(a, b));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# python3 program to split two
# binary strings in such a way
# such that sum is maximum.
 
# Function to split strings
# to find maximum sum
def MaxSum(a, b):
 
    ans = 0
    for i in range(0, len(a)):
        if (a[i] != b[i]):
            ans += 2
 
    n = len(a)
 
    # Traverse the strings.
    i = 0
    while i < n:
        if (a[i] == b[i]):
            if (a[i] == '0'):
 
                # If Ai is not equal to Bi,
                # increment the MaxSum by 2
                if (i + 1 < n and a[i + 1] == '1'
                        and b[i + 1] == '1'):
                    ans, i = ans + 2, i + 1
 
                # Else if the pair is only (0, 0)
                # increment MaxSum by 1.
                else:
                    ans += 1
 
            else:
                if (i + 1 < n and a[i + 1] == '0'
                        and b[i + 1] == '0'):
                    ans, i = ans + 2, i + 1
 
                else:
                    ans += 0
        i += 1
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    a = "0101000"
    b = "1101100"
    print(MaxSum(a, b))
 
    # This code is contributed by rakeshsahni


C#
// C# program to split two
// binary strings in such a way
// such that sum is maximum.
using System;
class GFG
{
 
  // Function to split strings
  // to find maximum sum
  static int MaxSum(string a, string b)
  {
    int ans = 0;
    for (int i = 0; i < a.Length; i++) {
      if (a[i] != b[i])
        ans += 2;
    }
    int n = a.Length;
 
    // Traverse the strings.
    for (int i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        if (a[i] == '0') {
 
          // If Ai is not equal to Bi,
          // increment the MaxSum by 2
          if (i + 1 < n && a[i + 1] == '1'
              && b[i + 1] == '1') {
            ans += 2;
            i++;
          }
 
          // Else if the pair is only (0, 0)
          // increment MaxSum by 1.
          else {
            ans += 1;
          }
        }
        else {
          if (i + 1 < n && a[i + 1] == '0'
              && b[i + 1] == '0') {
            ans += 2;
            i++;
          }
          else {
            ans += 0;
          }
        }
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
 
    string a = "0101000";
    string b = "1101100";
    Console.Write(MaxSum(a, b));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
8

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