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

📅  最后修改于: 2021-09-06 17:46:45             🧑  作者: Mango

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

例子:

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

  1. 如果 X < Y:交换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


Javascript


输出:
15

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live