C++ 最大化从字符串S中重复删除字符串P或其反转的成本
给定随机变量,即一对字符串a和b,即X和Y,保存成本。我们被要求执行最小总价格的任务,在成功从字符串Y中删除字符串X并将字符串X反转后,以给定的成本a和b获得。
C++代码
// Here we are writing down the C++ programming language code to
// demonstrate the concept of Maximize cost of repeated removal of string P
// or it's reverse from the string S
#include
using namespace std;
// This is the function that helps us with finding out the maximum cost of
// removing the substrings "hg" and "gh" from the string input S for us
int MaxCollection(string S, int P, int Q)
{
// The MaxStr function helps us with the function. It is the substring char
// array with higher cost in our function code written
char maxstr[2];
string x = (P >= Q ? "ab" : "ba");
strcpy(maxstr, x.c_str());
// The MaxStr function helps us with the function. It is the substring char
// array with a smaller cost in our function code written
char minstr[2];
x = (P >= Q ? "ba" : "ab");
strcpy(minstr, x.c_str());
// the below code snippet helps us with denoting the larger point code
int maxp = max(P, Q);
// the below code helps us with denoting the minor point in the code
int minp = min(P, Q);
// the below code snippet helps us with storing the cost-scored code
int cost = 0;
// The below small code snippet helps us with Removing all occurrences of
// the master from the string S given for us as in the form of input
// the below code snippet helps us to create the Stack data structure // to, which keeps track of characters in the code functionality
stack stack1;
char s[S.length()];
strcpy(s, S.c_str());
// here, we are trying to create a loop that can traverse the string
// using the in-built auto code in the Standard Template Library(STL)
for (auto &ch : s) {
// this is a checking condition if the substring is the same as maxstr
if (!stack1.empty()
&& (stack1.top() == maxstr[0]
&& ch == maxstr[1])) {
// deleting using the Pop operation from the stack in our code
stack1.pop();
// Here, we are trying to add the map to the cost, ensuring code
cost += maxp;
}
// the below conditional Pushes the character to the stack code
else {
stack1.push(ch);
// Pushing operation has been completed
}
}
// Now, the Remaining part of the string after we have removed the
// max function, we are storing the result in sb
string sb = "";
// Here we are trying to find out the remaining string leftover code
while (stack1.size() > 0)
{
sb = sb + stack1.top();
stack1.pop();
}
// Here, we are trying to reverse the string and perform certainly
// operations which have been retrieved from the stack data structure
reverse(sb.begin(), sb.end());
// Here we are trying to Remove all occurrences of the string minster
// the for loop which we have created below helps us with looping code
for (auto &ch : sb) {
// the if conditional checks if the substring is ministry or not
if (!stack1.empty()
&& (stack1.top() == minstr[0]
&& ch == minstr[1])) {
// performing the Pop operation from the stack data structure
stack1.pop();
// Here, we are trying to add the map to the cost in our code
cost += maxp;
}
// the else conditional implements the below code if the above
// code fails using the small code snippet
else {
stack1.push(ch);
}
}
// Here, we are trying to return the maximum cost to the main driver
// code functionality after the function completes its task
return cost;
}
// The main driver code functionality starts from here, explaining the
// concept of Maximize cost of repeated removal of string P or its reverse
// from the string S
int main()
{
// This is the input string where we will be performing the task on
string S = "cbbaabbaab";
// the steps to perform the string operation are written below
int P = 6;
int Q = 4;
// Here we are trying to display the final result after we have
// performed the task of Maximize cost of repeated removal of string P
// or it's reverse from the string S
cout << MaxCollection(S, P, Q);
return 0;
}
// End of the main driver code functionality explaining the concept of
// Maximize the cost of repeated removal of string P or its reverse from the
// string S
输出:
22