📜  使用添加、删除和追加操作形成 N-copy字符串

📅  最后修改于: 2021-09-17 07:43:04             🧑  作者: Mango

当一个字符串由字母“G”的 N 个副本组成时,它被称为 N 副本字符串。即“GGGG”是一个 4 个副本的字符串,因为它包含字母 ‘G’ 的 4 个副本。最初我们有一个空字符串,我们可以对它执行以下三个操作:

  1. 添加一个成本为 X 的字母“G”。
  2. 删除成本为 X 的单个字母“G”。
  3. 到目前为止形成的字符串附加到自身,成本为 Y,即我们有字符串’GG’,我们可以将它附加到自身以生成 ‘GGGG’
    给定 N、X 和 Y,使用上述操作找到生成 N-copy字符串的最小成本

例子:

该问题可以使用动态规划方法解决。
如果我们仔细分析这些操作,很容易看出我们只有在存在比所需副本多的副本时才执行删除操作,这只能是在此删除操作之前涉及类型 3 的操作时,因为如果在它之前有一个类型为 1 的操作,那么它是没有意义的,因为连续的添加和删除操作只会增加成本,而不会在我们的字符串引入任何新的副本。
因此我们可以说我们有以下操作,
1) 添加单个字符’G’,成本为 X
2) 将字符串附加到自身,然后删除单个字符’G’,成本为 Y + X
3) 将字符串附加到自身,成本为 Y
注意:现在我们参考这些修改后的操作
现在,它可以用 O(n) DP 方法解决。

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)