Form N-copy string with add, remove and append operations

A string is called a N-copy string when it is formed by N copies of letter ‘G’. ie “GGGG” is a 4-copy string since it contains 4 copies of the letter ‘G’. Initially we have a empty string on which we can perform the following three operations:

  1. Add a single letter ‘G’ with a cost of X.
  2. Remove a single letter ‘G’ with a cost of X.
  3. Append the string formed till now to itself with a cost of Y, i.e we have the string ‘GG’ we can append it to itself to make ‘GGGG’
    Given N, X and Y find the minimum cost to generate the N-copy string using the above operations

Examples:

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

The Problem can be solved using a Dynamic Programming Approach.
If we carefully analyse the operations, it is easy to see that we perform the remove operation only when there is an extra copy than what is required, this can only be the case when before this remove operation there was a operation of type 3 involved, because if there was an operation of type 1 before it, then it is meaningless as consecutive add and remove operations only increase the cost, without introducing any new copies into our string.
Thus we can say we have the following operations,
1) Add a single character ‘G’, with cost X
2) Append the string to itself and then remove a single character ‘G’, with cost Y + X
3) Append the string to itself, with cost Y
Note: Now on we refer to these modified operations

Now, it can be solved with a O(n) DP approach.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to find minimum cost to form a 
// N-copy string
#include <bits/stdc++.h>
  
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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 ?>


Output:

4

Time Complexity O(n)
Auxiliary Space O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.