分别给出两个正整数X和Y以及两个数字字符串S和长度为N P和2中,任务是找到通过重复地去除字符串P或从字符串S字符串P的在成本反向获得的最大总成本分别为X和Y。
例子:
Input: S = “12333444”, X = 5, Y = 4, P = “34”
Output: 15
Explanation:
Below are the operations to remove substring to get the maximum cost:
- Remove the string “34″ from the string S. Therefore, the string S modifies to “123344”. Cost = 5.
- Remove the string “34″ from the string S. Therefore, the string S modifies to “1234”. Cost = 5.
- Remove the string “34″ from the string S. Therefore, the string S modifies to “12”. Cost = 5.
Therefore, the total cost is 5 + 5 + 5 = 15.
Input: S = “12121221”, X = 7, Y = 10, P = “12”
Output: 37
方法:可以使用贪婪方法和堆栈来解决给定的问题。请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0来存储删除给定子串的最大成本。
- 初始化被用来决定字符串P或P的反向是否被去除的堆栈。
- 遍历给定的字符串S并执行以下步骤:
- 如果当前字符和顶部字符分别不等于P[1]和P[0]或者堆栈为空,则将当前字符压入堆栈。
- 否则,删除堆栈的顶部元素并将值ans增加X 。
- 类似地,通过将Y添加到成本中,可以从任何字符串删除字符串P的反向。
- 现在,如果X大于Y ,则在删除P之前 去掉P的反面会得到一个更大的值。否则,首先删除模式P的反向。
- 完成上述步骤后,打印ans的值作为总最大成本。
下面是上述方法的实现:
Python3
# Python program for the above approach
# Function to remove reverse of P first
def rp(string, x, y, ans, flag, p, r):
# Initialize empty stack
stack = []
# Iterate through each char in string
for char in string:
# Remove reverse of P
if char == p:
if stack and stack[-1] == r:
ans[0] += y
# Pop the element from
# the stack
stack.pop()
else:
stack.append(char)
else:
stack.append(char)
# Remove P, if needed
if flag:
pr("".join(stack), x, y, ans, False, p, r)
# Function to remove the string P first
def pr(string, x, y, ans, flag, p, r):
# Initialize empty stack
stack = []
# Iterate through each char
# in string
for char in string:
# Remove the string P
if char == r:
if stack and stack[-1] == p:
ans[0] += x
stack.pop()
# If no such pair exists just
# push the chars to stack
else:
stack.append(char)
else:
stack.append(char)
# Remove reverse of P, if needed
if flag:
rp("".join(stack), x, y, ans, False, p, r)
# Function to find the appropriate answer
def solve(string, x, y, p, r):
# Initialize amount
ans = [0]
# Remove P then reverse of P
if x > y:
pr(string, x, y, ans, True, p, r)
# Remove reverse of P then P
else:
rp(string, x, y, ans, True, p, r)
# Return the result
return ans[0]
# Driver Code
# Given string S
S = "12333444"
# Given X and Y
x = 5
y = 4
# Given P
P = "34"
# Function Call
print(solve(S, x, y, P[0], P[1]))
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG {
// Initialize amount
static int[] ans = {0};
// Function to remove the string P first
static void pr(string str, int x, int y, bool flag, char p, char r) {
// Initialize empty stack
Stack stack = new Stack();
// Iterate through each char
// in string
foreach(char c in str) {
// Remove the string P
if (c == r) {
if (stack.Count > 0 && (char)stack.Peek() == p) {
ans[0] += x;
stack.Pop();
}
// If no such pair exists just
// push the chars to stack
else {
stack.Push(c);
}
}
else {
stack.Push(c);
}
ans[0]+=1;
// Remove reverse of P, if needed
if (flag) {
string s = "";
Stack s1 = stack;
while (s1.Count > 0) {
s = s + (char)s1.Peek();
s1.Pop();
}
rp(s, x, y, false, p, r);
}
}
}
// Function to remove reverse of P first
static void rp(string str, int x, int y, bool flag, char p, char r)
{
// Initialize empty stack
Stack stack = new Stack();
// Iterate through each char in string
foreach(char c in str)
{
// Remove reverse of P
if (c == p) {
if (stack.Count > 0 && (char)stack.Peek() == r)
{
ans[0] += y;
// Pop the element from
// the stack
stack.Pop();
}
else
{
stack.Push(c);
}
}
else
{
stack.Push(c);
}
// Remove P, if needed
ans[0]+=1;
if (flag)
{
string s = "";
Stack s1 = stack;
while (s1.Count > 0) {
s = s + (char)s1.Peek();
s1.Pop();
}
pr(s, x, y, false, p, r);
}
}
}
// Function to find the appropriate answer
static int solve(string str, int x, int y, char p, char r) {
// Remove P then reverse of P
if (x > y)
pr(str, x, y, true, p, r);
// Remove reverse of P then P
else
rp(str, x, y, true, p, r);
// Return the result
return ans[0]-1;
}
static void Main()
{
// Given string S
string S = "12333444";
// Given X and Y
int x = 5;
int y = 4;
// Given P
string P = "34";
// Function Call
Console.Write(solve(S, x, y, P[0], P[1]));
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
15
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。