-
Notifications
You must be signed in to change notification settings - Fork 0
/
potd_22_3.cpp
59 lines (53 loc) · 1.53 KB
/
potd_22_3.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
long long remove2(string &s,string b,stack<char> &temp1)
{
//b="rp"
long long int count2=0;
stack <char>temp2;
temp2.push(temp1.top());
temp1.pop();
while(!temp1.empty())
{
if(!temp2.empty()&&(temp1.top()==b[0]&&temp2.top()==b[1]))
{
temp1.pop();
temp2.pop();
count2++;
}
else
{
temp2.push(temp1.top());
temp1.pop();
}
}
return count2;
}
long long remove1(string &s,string a, string b,int X,int Y)
{
//a="pr"
//b="rp"
int n =s.size();
long long int count1=0,count2=0;
stack<char>temp1;
temp1.push(s[0]);
for(int i=1;i<n;i++)
{
if(!temp1.empty()&&(temp1.top()==a[0]&&s[i]==a[1]))
{
temp1.pop();
count1++;
}
else temp1.push(s[i]);
}
count2=remove2(s,b,temp1);
return count1*X+count2*Y;
}
long long solve(int X,int Y,string S){
//code here
string string1="pr";
string string2="rp";
if(X>Y) return remove1(S,string1,string2,X,Y);
else
{
swap(X,Y);
return remove1(S,string2,string1,X,Y);
}