由N个字母“ G”副本组成的字符串称为N复制字符串。即“ GGGG”是一个4副本字符串,因为它包含4个字母“ G”的副本。最初,我们有一个空字符串,可以对其执行以下三个操作:
- 添加单个字母“ G”,成本为X。
- 删除成本为X的单个字母“ G”。
- 将到目前为止形成的字符串以Y的成本附加到其自身,即,我们具有字符串“ GG”,可以将其附加到其自身以制成“ GGGG”
给定N,X和Y,可以找到使用上述操作生成N复制字符串的最低成本
例子:
Input : N = 4, X = 2, Y = 1
Output : 4
Explanation:
1) Initially, the string it empty, we perform one Add operation to form string ‘G’.
2) Next, we perform a operation of type 3, and form the string ‘GG’.
3) Again we perform the operation 3 and form ‘GGGG’, which is the required N-copy string.
Cost = X + Y + Y = 2 + 1 + 1 = 4
Input : N = 4, X = 1, Y = 3
Output : 4
We can perform 4 consecutive Add operations to form ‘GGGG’.
Cost = X + X + X + X = 1 + 1 + 1 + 1 = 4
可以使用动态编程方法解决该问题。
如果我们仔细分析这些操作,很容易看出,仅当存在多余的副本时才执行删除操作,只有在此删除操作之前涉及到类型3的操作时,才会出现这种情况,因为如果前面有一个类型为1的操作,那么它是没有意义的,因为连续的add和remove操作只会增加成本,而不会在我们的字符串引入任何新的副本。
因此,我们可以说我们有以下操作,
1)添加单个字符“ G”,成本为X
2)将字符串附加到其自身,然后删除单个字符“ G”,成本为Y + X
3)将字符串附加到其自身,成本为Y
注意:现在我们将参考这些修改后的操作
现在,可以使用O(n)DP方法解决它。
Let dp[i] represent the minimum cost to form a i-copy string
then the state transitions can be:
1) If i is odd,
Case 1: Add a character to the (i-1)-copy string,
Case 2: Perform an operation of type 2 on the ((i+1)/2)-copy string
i.e dp[i] = min(dp[i – 1] + X, dp[(i + 1) / 2)] + Y + X)
2) If i is even
Case 1: Add a character to the (i-1)-copy string,
Case 2: Perform an operation of type 3 on the (i/2)-copy string
i.e dp[i] = min(dp[i – 1] + X, dp[i / 2] + Y)
C++
// CPP code to find minimum cost to form a
// N-copy string
#include
using namespace std;
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
int findMinimumCost(int n, int x, int y)
{
int* dp = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++) {
if (i & 1) {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
}
else {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = min(dp[i - 1] + x, dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
int main()
{
int n = 4, x = 2, y = 1;
cout << findMinimumCost(n, x, y);
return 0;
}
Java
// Java code to find minimum cost to form a
// N-copy string
class Solution
{
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
static int findMinimumCost(int n, int x, int y)
{
int dp[] = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++) {
if ((i & 1)!=0) {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
}
else {
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = Math.min(dp[i - 1] + x, dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
public static void main(String args[])
{
int n = 4, x = 2, y = 1;
System.out.println( findMinimumCost(n, x, y));
}
}
//contributed by Arnab Kundu
Python3
# Python3 code to find minimum cost to
# form a N-copy string
# function returns the minimum cost to
# form a n-copy string Here, x->Cost to
# add/remove a single character 'G' and
# y->cost to append the string to itself
def findMinimumCost(n, x, y):
dp = [0 for i in range(n + 1)]
# base case: ro form a 1-copy string
# we need tp perform an operation
# of type 1(i,e Add)
dp[1] = x
for i in range(2, n + 1):
if i & 1:
# case1. Perform a Add operation
# on (i-1)copy string
# case2. perform a type 2 operation
# on((i+1)/2)-copy string
dp[i] = min(dp[i - 1] + x,
dp[(i + 1) // 2] + y + x)
else:
# case1. Perform a Add operation
# on (i-1)-copy string
# case2. Perform a type # operation
# on (i/2)-copy string
dp[i] = min(dp[i - 1] + x,
dp[i // 2] + y)
return dp[n]
# Driver code
n, x, y = 4, 2, 1
print(findMinimumCost(n, x, y))
# This code is contributed
# by Mohit Kumar
C#
// C# code to find minimum cost to form a
// N-copy string
using System;
class GFG
{
// Returns the minimum cost to form a n-copy string
// Here, x -> Cost to add/remove a single character 'G'
// and y-> cost to append the string to itself
static int findMinimumCost(int n, int x, int y)
{
int[] dp = new int[n + 1];
// Base Case: to form a 1-copy string we
// need to perform an operation of type
// 1(i.e Add)
dp[1] = x;
for (int i = 2; i <= n; i++)
{
if ((i & 1)!=0)
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
dp[i] = Math.Min(dp[i - 1] + x,
dp[(i + 1) / 2] + y + x);
}
else
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
dp[i] = Math.Min(dp[i - 1] + x,
dp[i / 2] + y);
}
}
return dp[n];
}
// Driver Code
public static void Main()
{
int n = 4, x = 2, y = 1;
Console.WriteLine(findMinimumCost(n, x, y));
}
}
// This code is contributed
// by Akanksha Rai
PHP
Cost to add/remove a
// single character 'G' and y-> cost to
// append the string to itself
function findMinimumCost($n, $x, $y)
{
$dp[$n + 1] = array();
// Base Case: to form a 1-copy string
// we need to perform an operation of
// type 1(i.e Add)
$dp[1] = $x;
for ($i = 2; $i <= $n; $i++)
{
if ($i & 1)
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 2 operation
// on ((i + 1) / 2)-copy string
$dp[$i] = min($dp[$i - 1] + $x,
$dp[($i + 1) / 2] + $y + $x);
}
else
{
// Case1. Perform a Add operation on
// (i-1)-copy string,
// Case2. Perform a type 3 operation on
// (i/2)-copy string
$dp[$i] = min($dp[$i - 1] + $x,
$dp[$i / 2] + $y);
}
}
return $dp[$n];
}
// Driver Code
$n = 4;
$x = 2;
$y = 1;
echo findMinimumCost($n, $x, $y);
// This code is contributed by Sach_Code
?>
Javascript
输出:
4
时间复杂度O(n)
辅助空间O(n)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。