📜  使两个字符串相同的最低成本

📅  最后修改于: 2021-06-25 20:27:10             🧑  作者: Mango

给定四个整数abcd和两个相等长度的字符串S1S2 ,它们仅由字符‘2’‘1’‘0’组成

  1. 转换“1”“2”,或反之亦然成本
  2. “ 2”转换为“ 3” ,反之亦然,则成本为b
  3. “ 3”转换为“ 1” ,反之亦然,则成本为c
  4. 两个字符串删除第i字符将花费d

任务是找到使两个字符串在配置上相等的最低成本。
例子:

方法:

  • 迭代字符串,如果s1 [i] = s2 [i],则不执行任何操作。
  • 如果s1 [i] =’1′s2 [i] =’2’,则执行以下操作的最小成本运算:
    1. 删除花费d的s1 [i]s2 [i]
    2. 变化“1”“2”,花费
    3. “ 1”更改为“ 3” ,然后将“ 3”更改为“ 2” ,这将花费b + c
  • 如果s1 [i] =’2′并且s2 [i] =’3’,则根据以下内容执行最小成本运算:
    1. 删除花费d的s1 [i]s2 [i]
    2. 将成本为b的“ 2”更改为“ 3”
    3. “ 2”更改为“ 1” ,然后将“ 1”更改为“ 3” ,这将花费a + c
  • 如果s1 [i] =’3′并且s2 [i] =’1’,则根据以下内容执行最小成本运算:
    1. 删除花费d的s1 [i]s2 [i]
    2. 将成本为c的“ 3”更改为“ 1”
    3. ‘3’更改为‘2’ ,然后将‘2’更改为‘1’ ,这将花费b + a

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum cost to make the
// configuration of both the strings same
int findCost(string s1, string s2,
             int a, int b, int c, int d, int n)
{
    int cost = 0;
 
    // Iterate and find the cost
    for (int i = 0; i < n; i++) {
        if (s1[i] == s2[i])
            continue;
        else {
 
            // Find the minimum cost
            if ((s1[i] == '1' && s2[i] == '2')
                || (s2[i] == '1' && s1[i] == '2'))
                cost += min(d, min(a, b + c));
            else if ((s1[i] == '2' && s2[i] == '3')
                     || (s2[i] == '2' && s1[i] == '3'))
                cost += min(d, min(b, a + c));
            else if ((s1[i] == '1' && s2[i] == '3')
                     || (s2[i] == '1' && s1[i] == '3'))
                cost += min(d, min(c, a + b));
        }
    }
    return cost;
}
 
// Driver Code
int main()
{
    string s1 = "121";
    string s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.size();
    cout << findCost(s1, s2, a, b, c, d, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the minimum cost to make the
// configuration of both the strings same
static int findCost(String s1, String s2,
                    int a, int b, int c,
                    int d, int n)
{
    int cost = 0;
 
    // Iterate and find the cost
    for (int i = 0; i < n; i++)
    {
        if (s1.charAt(i) == s2.charAt(i))
            continue;
        else
        {
 
            // Find the minimum cost
            if ((s1.charAt(i) == '1' && s2.charAt(i) == '2') ||
                (s2.charAt(i) == '1' && s1.charAt(i) == '2'))
                cost += Math.min(d, Math.min(a, b + c));
            else if ((s1.charAt(i) == '2' && s2.charAt(i) == '3') ||
                     (s2.charAt(i) == '2' && s1.charAt(i) == '3'))
                cost += Math.min(d, Math.min(b, a + c));
            else if ((s1.charAt(i) == '1' && s2.charAt(i) == '3') ||
                     (s2.charAt(i) == '1' && s1.charAt(i) == '3'))
                cost += Math.min(d, Math.min(c, a + b));
        }
    }
    return cost;
}
 
// Driver Code
public static void main(String[] args)
{
    String s1 = "121";
    String s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.length();
    System.out.println(findCost(s1, s2, a, b, c, d, n));
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python 3 implementation of the approach
 
# Function to return the minimum cost to make
# the configuration of both the strings same
def findCost(s1, s2, a, b, c, d, n):
    cost = 0
 
    # Iterate and find the cost
    for i in range(n):
        if (s1[i] == s2[i]):
            continue
        else:
             
            # Find the minimum cost
            if ((s1[i] == '1' and s2[i] == '2') or
                (s2[i] == '1' and s1[i] == '2')):
                cost += min(d, min(a, b + c))
            elif ((s1[i] == '2' and s2[i] == '3') or
                  (s2[i] == '2' and s1[i] == '3')):
                cost += min(d, min(b, a + c))
            elif ((s1[i] == '1' and s2[i] == '3') or
                  (s2[i] == '1' and s1[i] == '3')):
                cost += min(d, min(c, a + b))
    return cost
 
# Driver Code
if __name__ == '__main__':
    s1 = "121"
    s2 = "223"
    a = 2
    b = 3
    c = 4
    d = 10
    n = len(s1)
    print(findCost(s1, s2, a, b, c, d, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the minimum cost to make the
// configuration of both the strings same
static int findCost(string s1, string s2,
            int a, int b, int c, int d, int n)
{
    int cost = 0;
 
    // Iterate and find the cost
    for (int i = 0; i < n; i++)
    {
        if (s1[i] == s2[i])
            continue;
        else
        {
 
            // Find the minimum cost
            if ((s1[i] == '1' && s2[i] == '2')
                || (s2[i] == '1' && s1[i] == '2'))
                cost +=Math.Min(d,Math.Min(a, b + c));
            else if ((s1[i] == '2' && s2[i] == '3')
                    || (s2[i] == '2' && s1[i] == '3'))
                cost +=Math.Min(d,Math.Min(b, a + c));
            else if ((s1[i] == '1' && s2[i] == '3')
                    || (s2[i] == '1' && s1[i] == '3'))
                cost +=Math.Min(d,Math.Min(c, a + b));
        }
    }
    return cost;
}
 
// Driver Code
public static void Main()
{
    string s1 = "121";
    string s2 = "223";
    int a = 2, b = 3, c = 4, d = 10;
    int n = s1.Length;
    Console.WriteLine(findCost(s1, s2, a, b, c, d, n));
}
}
 
// This Code is Contributed by Code_Mech.


Javascript


输出:
6

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。