-
Notifications
You must be signed in to change notification settings - Fork 2
/
1066.cpp
41 lines (34 loc) · 1 KB
/
1066.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int clist[26], state[2005][2005];
string ori_name;
int N, M;
int CalCost(int l, int r) {
int i, tem_cost1, tem_cost2, left, right;
if (l >= r) return 0;
if (ori_name[l] == ori_name[r]) return CalCost(++l,--r);
if (state[l][r] >= 0) return state[l][r];
left = ori_name[l] - 'a';
right = ori_name[r] - 'a';
tem_cost1 = clist[left] + CalCost(l + 1, r);
tem_cost2 = clist[right] + CalCost(l, r - 1);
int min_cost = tem_cost1 < tem_cost2 ? tem_cost1 : tem_cost2;
state[l][r] = min_cost;
return min_cost;
}
int main(){
cin >> N >> M >> ori_name;
for (int i = 0; i < M; ++i)
for (int j = 0; j < M; ++j) state[i][j] = -1;
for (int i = 0; i < N; ++i) {
int acost, dcost;
char cha;
cin >> cha >> acost >> dcost;
clist[cha - 'a'] = acost < dcost ? acost : dcost;
}
int min_cost = CalCost(0, M - 1);
cout << min_cost;
return 0;
}