-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.cpp
49 lines (41 loc) · 839 Bytes
/
pr.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
#include<bits/stdc++.h>
using namespace std;
int n,pr,rp;
struct node {
int val=-1;
};
unordered_map<string,node> dp;
int helper(string s) {
if(s=="") {
return 0;
}
if(dp[s].val != -1) {
return dp[s].val;
}
int ret=0;
for(int i=0;i<s.size()-1;i++) {
if(s[i]=='p' && s[i+1]=='r') {
string t = s.substr(0,i) + (i==n-2?"":s.substr(i+2));
ret = max(ret,pr+helper(t));
}
if(s[i]=='r' && s[i+1]=='p') {
string t = s.substr(0,i) + (i==n-2?"":s.substr(i+2));
ret= max(ret, rp+helper(t));
}
}
return dp[s].val = ret;
}
void solve() {
string s;
cin>>s>>pr>>rp;
int ret = helper(s);
cout<<ret<<endl;
}
int main() {
int t;
cin>>t;
while(t--) {
solve();
}
return 0;
}