📌  相关文章
📜  通过从给定字符串中删除子字符串“ pr”或“ rp”而获得的成本最大化

📅  最后修改于: 2021-04-21 21:25:42             🧑  作者: Mango

给定一个字符串str和两个整数XY ,任务是找到从给定字符串删除所有子字符串“ pr”和“ rp”所需的最大成本,其中删除子字符串“ rp”和“ pr”的成本为XY分别。

例子:

方法:可以使用贪婪方法解决问题。这里的想法是,如果X大于Y ,则删除“ pr ”,否则,删除“ rp” 。请按照以下步骤解决问题。

  1. 如果X 交换XY的值,并在给定的字符串中将字符‘p’替换为‘r’ ,反之亦然。
  2. 初始化两个变量countPcountR分别将‘p’‘r’的计数存储在字符串。
  3. 遍历数组arr []并执行以下步骤:
    • 如果str [i] =’p’:countP加1。
    • 如果str [i] =’r’:检查countP的值。如果countP> 0 ,则将结果增加X并将countP的值减少1。否则,将countR的值增加1。
    • 如果str [i]!=’p’和str [i]!=’r’:将结果增加min(countP,countR)* Y。
  4. 将结果递增min(countP,countR)* Y。
  5. 最后,在除去所有必需的子字符串之后,打印获得的结果。
C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to maintain the case, X>=Y
bool swapXandY(string& str, int X, int Y)
{
 
    int N = str.length();
 
    // To maintain X>=Y
    swap(X, Y);
 
    for (int i = 0; i < N; i++) {
 
        // Replace 'p' to 'r'
        if (str[i] == 'p') {
            str[i] = 'r';
        }
 
        // Replace 'r' to 'p'.
        else if (str[i] == 'r') {
            str[i] = 'p';
        }
    }
}
 
// Function to return the maximum cost
int maxCost(string str, int X, int Y)
{
    // Stores the length of the string
    int N = str.length();
 
    // To maintain X>=Y.
    if (Y > X) {
        swapXandY(str, X, Y);
    }
 
    // Stores the maximum cost
    int res = 0;
 
    // Stores the count of 'p'
    // after removal of all "pr"
    // substrings up to str[i]
    int countP = 0;
 
    // Stores the count of 'r'
    // after removal of all "pr"
    // substrings up to str[i]
    int countR = 0;
 
    // Stack to maintain the order of
    // characters after removal of
    // substrings
    for (int i = 0; i < N; i++) {
 
        if (str[i] == 'p') {
            countP++;
        }
        else if (str[i] == 'r') {
 
            // If substring "pr"
            // is removed
            if (countP > 0) {
                countP--;
 
                // Increase cost by X
                res += X;
            }
            else
                countR++;
        }
        else {
 
            // If any substring "rp"
            // left in the Stack
            res += min(countP, countR) * Y;
            countP = 0;
            countR = 0;
        }
    }
 
    // If any substring "rp"
    // left in the Stack
    res += min(countP, countR) * Y;
    return res;
}
 
// Driver Code
int main()
{
    string str = "abppprrr";
    int X = 5, Y = 4;
    cout << maxCost(str, X, Y);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to maintain the case, X>=Y
static boolean swapXandY(char []str, int X, int Y)
{
    int N = str.length;
 
    // To maintain X>=Y
    X = X + Y;
    Y = X - Y;
    X = X - Y;
 
    for(int i = 0; i < N; i++)
    {
 
        // Replace 'p' to 'r'
        if (str[i] == 'p')
        {
            str[i] = 'r';
        }
 
        // Replace 'r' to 'p'.
        else if (str[i] == 'r')
        {
            str[i] = 'p';
        }
    }
    return true;
}
 
// Function to return the maximum cost
static int maxCost(String str, int X, int Y)
{
     
    // Stores the length of the String
    int N = str.length();
 
    // To maintain X>=Y.
    if (Y > X)
    {
        swapXandY(str.toCharArray(), X, Y);
    }
 
    // Stores the maximum cost
    int res = 0;
 
    // Stores the count of 'p'
    // after removal of all "pr"
    // subStrings up to str[i]
    int countP = 0;
 
    // Stores the count of 'r'
    // after removal of all "pr"
    // subStrings up to str[i]
    int countR = 0;
 
    // Stack to maintain the order of
    // characters after removal of
    // subStrings
    for(int i = 0; i < N; i++)
    {
        if (str.charAt(i) == 'p')
        {
            countP++;
        }
        else if (str.charAt(i) == 'r')
        {
             
            // If subString "pr"
            // is removed
            if (countP > 0)
            {
                countP--;
 
                // Increase cost by X
                res += X;
            }
            else
                countR++;
        }
        else
        {
 
            // If any subString "rp"
            // left in the Stack
            res += Math.min(countP, countR) * Y;
            countP = 0;
            countR = 0;
        }
    }
 
    // If any subString "rp"
    // left in the Stack
    res += Math.min(countP, countR) * Y;
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "abppprrr";
    int X = 5, Y = 4;
     
    System.out.print(maxCost(str, X, Y));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to maintain the case, X>=Y
def swapXandY(str, X, Y):
 
    N = len(str)
 
    # To maintain X>=Y
    X, Y = Y, X
 
    for i in range(N):
 
        # Replace 'p' to 'r'
        if(str[i] == 'p'):
            str[i] = 'r'
 
        # Replace 'r' to 'p'.
        elif(str[i] == 'r'):
            str[i] = 'p'
 
# Function to return the maximum cost
def maxCost(str, X, Y):
 
    # Stores the length of the string
    N = len(str)
 
    # To maintain X>=Y.
    if(Y > X):
        swapXandY(str, X, Y)
 
    # Stores the maximum cost
    res = 0
 
    # Stores the count of 'p'
    # after removal of all "pr"
    # substrings up to str[i]
    countP = 0
 
    # Stores the count of 'r'
    # after removal of all "pr"
    # substrings up to str[i]
    countR = 0
 
    # Stack to maintain the order of
    # characters after removal of
    # substrings
    for i in range(N):
        if(str[i] == 'p'):
            countP += 1
 
        elif(str[i] == 'r'):
 
            # If substring "pr"
            # is removed
            if(countP > 0):
                countP -= 1
 
                # Increase cost by X
                res += X
 
            else:
                countR += 1
 
        else:
             
            # If any substring "rp"
            # left in the Stack
            res += min(countP, countR) * Y
            countP = 0
            countR = 0
 
    # If any substring "rp"
    # left in the Stack
    res += min(countP, countR) * Y
 
    return res
 
# Driver Code
str = "abppprrr"
X = 5
Y = 4
 
# Function call
print(maxCost(str, X, Y))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to maintain the case, X>=Y
static bool swapXandY(char []str,
                      int X, int Y)
{
    int N = str.Length;
 
    // To maintain X>=Y
    X = X + Y;
    Y = X - Y;
    X = X - Y;
 
    for(int i = 0; i < N; i++)
    {
        // Replace 'p' to 'r'
        if (str[i] == 'p')
        {
            str[i] = 'r';
        }
 
        // Replace 'r' to 'p'.
        else if (str[i] == 'r')
        {
            str[i] = 'p';
        }
    }
    return true;
}
 
// Function to return the
// maximum cost
static int maxCost(String str,
                   int X, int Y)
{   
    // Stores the length of the String
    int N = str.Length;
 
    // To maintain X>=Y.
    if (Y > X)
    {
        swapXandY(str.ToCharArray(),
                  X, Y);
    }
 
    // Stores the maximum cost
    int res = 0;
 
    // Stores the count of 'p'
    // after removal of all "pr"
    // subStrings up to str[i]
    int countP = 0;
 
    // Stores the count of 'r'
    // after removal of all "pr"
    // subStrings up to str[i]
    int countR = 0;
 
    // Stack to maintain the order of
    // characters after removal of
    // subStrings
    for(int i = 0; i < N; i++)
    {
        if (str[i] == 'p')
        {
            countP++;
        }
        else if (str[i] == 'r')
        {           
            // If subString "pr"
            // is removed
            if (countP > 0)
            {
                countP--;
 
                // Increase cost by X
                res += X;
            }
            else
                countR++;
        }
        else
        {
            // If any subString "rp"
            // left in the Stack
            res += Math.Min(countP,
                            countR) * Y;
            countP = 0;
            countR = 0;
        }
    }
 
    // If any subString "rp"
    // left in the Stack
    res += Math.Min(countP,
                    countR) * Y;
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "abppprrr";
    int X = 5, Y = 4;   
    Console.Write(maxCost(str, X, Y));
}
}
 
// This code is contributed by 29AjayKumar


输出:
15



时间复杂度: O(N),其中N表示字符串的长度
辅助空间: O(1)