-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1717.cpp
33 lines (31 loc) · 987 Bytes
/
1717.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://leetcode.com/problems/maximum-score-from-removing-substrings/?envType=daily-question&envId=2024-07-12
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maximumGain(string s, int x, int y) {
int ans = 0;
stack<char> st;
string remove = (x > y ? "ab" : "ba");
for (char c : s) {
if (!st.empty() && st.top() == remove[0] && c == remove[1]) {
ans += max(x,y);
st.pop();
} else st.push(c);
}
string tmp = "";
while (!st.empty()) {
tmp += st.top(); st.pop();
}
// cout << ans << ' ' << tmp << '\n';
reverse(tmp.begin(), tmp.end());
remove = (x > y ? "ba" : "ab");
for (char c : tmp) {
if (!st.empty() && st.top() == remove[0] && c == remove[1]) {
ans += min(x,y);
st.pop();
} else st.push(c);
}
return ans;
}
};