给定四个整数a , b , c , d和两个相等长度的字符串S1和S2 ,它们仅由字符‘2’ , ‘1’和‘0’组成。
- 转换“1”到“2”,或反之亦然成本。
- 将“ 2”转换为“ 3” ,反之亦然,则成本为b 。
- 将“ 3”转换为“ 1” ,反之亦然,则成本为c 。
- 从两个字符串删除第i个字符将花费d 。
任务是找到使两个字符串在配置上相等的最低成本。
例子:
Input: s1 = “121”, s2 = “223”, a = 2, b = 3, c = 4, d = 10
Output: 6
Change the first character from ‘1’ to ‘2’ which costs 2.
Change the third character from ‘3’ to ‘1’ which costs 4.
Input: s1 = ‘222’, s2 = ‘111’, a = 20, b = 30, c = 40, d = 1
Output: 3
Delete all characters which costs 3 which is the minimum possible.
方法:
- 迭代字符串,如果s1 [i] = s2 [i],则不执行任何操作。
- 如果s1 [i] =’1′且s2 [i] =’2’,则执行以下操作的最小成本运算:
- 删除花费d的s1 [i]和s2 [i] 。
- 变化“1”到“2”,这花费
- 将“ 1”更改为“ 3” ,然后将“ 3”更改为“ 2” ,这将花费b + c 。
- 如果s1 [i] =’2′并且s2 [i] =’3’,则根据以下内容执行最小成本运算:
- 删除花费d的s1 [i]和s2 [i] 。
- 将成本为b的“ 2”更改为“ 3” 。
- 将“ 2”更改为“ 1” ,然后将“ 1”更改为“ 3” ,这将花费a + c 。
- 如果s1 [i] =’3′并且s2 [i] =’1’,则根据以下内容执行最小成本运算:
- 删除花费d的s1 [i]和s2 [i] 。
- 将成本为c的“ 3”更改为“ 1” 。
- 将‘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现场课程美国》。