文章目录
-
- 原题题目
- 代码实现(首刷看了点思路 没理解怎么证明的)
原题题目
代码实现(首刷看了点思路 没理解怎么证明的)
class Solution {
public:int maximumGain(string s, int x, int y) {
stack<char> stack,stacktemp;bool flag = x>y;int xcount = 0,ycount = 0;for(int i=0;i<s.size();++i){
bool temp = false;if(stack.size()){
if((flag && stack.top() == 'a' && s[i] == 'b') || (!flag && stack.top() == 'b' && s[i] == 'a')){
stack.pop();temp = true;if(flag) ++xcount;else ++ycount;}}if(!temp) stack.push(s[i]);}char pre = '0';while(!stack.empty()){
bool judge = false;auto temp = stack.top();stack.pop();if(flag && stacktemp.size() && stacktemp.top() == 'a' && temp == 'b'){
++ycount;stacktemp.pop();judge ^= true;}else if(!flag && stacktemp.size() && stacktemp.top() == 'b' && temp == 'a'){
++xcount;stacktemp.pop();judge ^= true;} if(!judge) stacktemp.push(temp);}return xcount*x + ycount*y;}
};