Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
string s;
int k, n, bn[27][27], dp[101][27][101];
int rec(int idx, int prv, int cur) {
if(idx == s.length())
return 0;
int &ret = dp[idx][prv][cur];
if(ret != -1e9)
return ret;
ret = max(ret, rec(idx + 1, s[idx] - 'a', cur) + bn[prv][s[idx] - 'a']);
for(int i = 0; cur < k && i < 26; ++i)
ret = max(ret, rec(idx + 1, i, cur + 1) + bn[prv][i]);
return ret;
}
void build_path(int idx, int prv, int cur) {
if(idx == s.length())
return;
int opt = rec(idx, prv, cur);
if(opt == rec(idx + 1, s[idx] - 'a', cur) + bn[prv][s[idx] - 'a']) {
cout << s[idx];
build_path(idx + 1, s[idx] - 'a', cur);
return;
}
for(int i = 0; cur < k && i < 26; ++i) {
if(opt == rec(idx + 1, i, cur + 1) + bn[prv][i]) {
cout << char(i + 'a');
build_path(idx + 1, i, cur + 1);
return;
}
}
}
int main() {
cin >> s >> k;
cin >> n;
for(int i = 0; i < n; ++i) {
char a, b;
int c;
cin >> a >> b >> c;
bn[a - 'a'][b -'a'] = c;
}
for(int i = 0; i < 101; ++i)
for(int j = 0; j < 27; ++j)
for(int k = 0; k < 101; ++k)
dp[i][j][k] = -1e9;
cout << rec(0, 26, 0) << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
winner 4
4
s e 7
o s 8
l o 13
o o 8
4
s e 7
o s 8
l o 13
o o 8
Output
36
Demonstration
Codeforcess Solution LionAge II, C. LionAge II C,C++, Java, Js and Python ,C. LionAge II,Codeforcess Solution