📌  相关文章
📜  最小化将给定字符串转换为 XYXY… 或 XXYY… 类型的成本

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

最小化将给定字符串转换为 XYXY… 或 XXYY… 类型的成本

给定一个二进制字符串s和一个偶数大小的数组ct[] 。任务是最小化运营成本,以:

  • 将字符串s转换为XYXYXYXYXY ... 或XXYYXXYYXXYY ... 类型的字符串
  • 在一个操作中,允许使用成本ct[i]翻转第 i字符。

例子

方法:这个问题是基于观察的。请按照以下步骤解决给定的问题。

  • 根据问题,只有四种类型的二进制字符串。
    • 010101010101……。
    • 101010101010……。
    • 001100110011……..
    • 110011001100……..
  • 我们只需要检查这四个不同的字符串。
  • 将字符串与 cost 和前四个预期字符一起传递。
  • 如果预期字符与字符串的实际字符不匹配,则添加与第i个值对应的成本。
  • 对所有四个预期序列重复该过程并从中选择最小成本。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
int solve_util(string s, int c[],
               char x, char y,
               char z, char w)
{
    int ans = 0;
    for (int i = 0; i < s.length(); i += 4) {
        if (s[i] != x)
            ans += c[i];
        if (i + 1 < s.length()
            && s[i + 1] != y)
            ans += c[i + 1];
        if (i + 2 < s.length()
            && s[i + 2] != z)
            ans += c[i + 2];
        if (i + 3 < s.length()
            && s[i + 3] != w)
            ans += c[i + 3];
    }
    return ans;
}
 
int solve_util2(string s, int c[],
                char x, char y)
{
    int ans = 0;
    if (s[0] != x)
        ans += c[0];
    if (s[1] != y)
        ans += c[1];
    return ans;
}
 
// Function to convert given
// string to required form
int minOperations(int N, string S, int C[])
{
    // code here
    if (S.length() == 2) {
        int x = solve_util2(
            S, C, '0', '1');
        int y = solve_util2(
            S, C, '1', '0');
        int z = solve_util2(
            S, C, '1', '1');
        int w = solve_util2(
            S, C, '0', '0');
        return min({ x, y, z, w });
    }
    int x = solve_util(
        S, C, '0', '1', '0', '1');
    int y = solve_util(
        S, C, '1', '0', '1', '0');
    int z = solve_util(
        S, C, '1', '1', '0', '0');
    int w = solve_util(
        S, C, '0', '0', '1', '1');
    return min({ x, y, z, w });
}
 
// Driver Code
int main()
{
    int N = 4;
    string s = "1011";
    int ct[] = { 1, 2, 1, 3 };
 
    cout << minOperations(N, s, ct) << "\n";
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
 
  static int solve_util(char s[], int c[],
                        char x, char y,
                        char z, char w)
  {
    int ans = 0;
    for (int i = 0; i < s.length; i += 4) {
      if (s[i] != x)
        ans += c[i];
      if (i + 1 < s.length
          && s[i + 1] != y)
        ans += c[i + 1];
      if (i + 2 < s.length
          && s[i + 2] != z)
        ans += c[i + 2];
      if (i + 3 < s.length
          && s[i + 3] != w)
        ans += c[i + 3];
    }
    return ans;
  }
 
  static int solve_util2(char s[], int c[],
                         char x, char y)
  {
    int ans = 0;
    if (s[0] != x)
      ans += c[0];
    if (s[1] != y)
      ans += c[1];
    return ans;
  }
 
  // Function to convert given
  // string to required form
  static int minOperations(int N, char S[], int C[])
  {
    // code here
    if (S.length == 2) {
      int x = solve_util2(
        S, C, '0', '1');
      int y = solve_util2(
        S, C, '1', '0');
      int z = solve_util2(
        S, C, '1', '1');
      int w = solve_util2(
        S, C, '0', '0');
      return Math.min(x, Math.min( y, Math.min(z, w )));
    }
    int x = solve_util(
      S, C, '0', '1', '0', '1');
    int y = solve_util(
      S, C, '1', '0', '1', '0');
    int z = solve_util(
      S, C, '1', '1', '0', '0');
    int w = solve_util(
      S, C, '0', '0', '1', '1');
    return Math.min(x, Math.min( y, Math.min(z, w )));
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 4;
    char s[] = {'1', '0', '1', '1'};
    int ct[] = { 1, 2, 1, 3 };
 
    System.out.print(minOperations(N, s, ct));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python program for above approach
def solve_util(s, c, x, y, z, w):
    ans = 0
    for i in range(0, len(s), 4):
        if (s[i] != x):
            ans += c[i]
        if (i + 1 < len(s)
                and s[i + 1] != y):
            ans += c[i + 1]
        if (i + 2 < len(s)
                and s[i + 2] != z):
            ans += c[i + 2]
        if (i + 3 < len(s)
                and s[i + 3] != w):
            ans += c[i + 3]
 
    return ans
 
def solve_util2(s, c, x, y):
    ans = 0
    if (s[0] != x):
        ans += c[0]
    if (s[1] != y):
        ans += c[1]
    return ans
 
# Function to convert given
# string to required form
def minOperations(N, S, C):
 
    # code here
    if (len(S) == 2):
        x = solve_util2(S, C, '0', '1')
        y = solve_util2(S, C, '1', '0')
        z = solve_util2(S, C, '1', '1')
        w = solve_util2(S, C, '0', '0')
        print(f"{x},{y},{z},{w}")
        return min([x, y, z, w])
 
    x = solve_util(S, C, '0', '1', '0', '1')
    y = solve_util(S, C, '1', '0', '1', '0')
    z = solve_util(S, C, '1', '1', '0', '0')
    w = solve_util(S, C, '0', '0', '1', '1')
 
    return min([x, y, z, w])
 
# Driver Code
N = 4
s = "1011"
ct = [1, 2, 1, 3]
 
print(minOperations(N, s, ct))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
 
  static int solve_util(char []s, int []c,
                        char x, char y,
                        char z, char w)
  {
    int ans = 0;
    for (int i = 0; i < s.Length; i += 4) {
      if (s[i] != x)
        ans += c[i];
      if (i + 1 < s.Length
          && s[i + 1] != y)
        ans += c[i + 1];
      if (i + 2 < s.Length
          && s[i + 2] != z)
        ans += c[i + 2];
      if (i + 3 < s.Length
          && s[i + 3] != w)
        ans += c[i + 3];
    }
    return ans;
  }
 
  static int solve_util2(char []s, int []c,
                         char x, char y)
  {
    int ans = 0;
    if (s[0] != x)
      ans += c[0];
    if (s[1] != y)
      ans += c[1];
    return ans;
  }
 
  // Function to convert given
  // string to required form
  static int minOperations(int N, char []S, int []C)
  {
    // code here
    if (S.Length == 2) {
      int x = solve_util2(
        S, C, '0', '1');
      int y = solve_util2(
        S, C, '1', '0');
      int z = solve_util2(
        S, C, '1', '1');
      int w = solve_util2(
        S, C, '0', '0');
      return Math.Min(x, Math.Min( y, Math.Min(z, w )));
    }
     
    else {
      int x = solve_util(
        S, C, '0', '1', '0', '1');
      int y = solve_util(
        S, C, '1', '0', '1', '0');
      int z = solve_util(
        S, C, '1', '1', '0', '0');
      int w = solve_util(
        S, C, '0', '0', '1', '1');
      return Math.Min(x, Math.Min( y, Math.Min(z, w )));
    }
  }
 
  // Driver Code
  public static void Main () {
    int N = 4;
    char []s = {'1', '0', '1', '1'};
    int []ct = { 1, 2, 1, 3 };
 
    Console.Write(minOperations(N, s, ct));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1

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