#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2000+5;
int dp[maxn][maxn];
char str[maxn];
int cost[maxn];
int n, m;
int main() {
//freopen("data.in", "r", stdin);
while(scanf("%d %d", &n, &m) == 2) {
scanf("%s", str);
char c[2];
int x, y;
for(int i = 0; i < n; i++) {
scanf("%s%d%d", c, &x, &y);
cost[c[0] - 'a'] = min(x, y);
}
for(int i = 0; i < m; ++i)
dp[i][i] = 0;
for(int k = 2; k <= m; ++k) {//区间长度
for(int i = 0; i < m - k + 1; i++) {
int j = i + k - 1;
dp[i][j] = 0x3f3f3f3f;
int mi = min(dp[i+1][j] + cost[str[i]-'a'], dp[i][j-1] + cost[str[j]-'a']);
dp[i][j] = min(mi, dp[i][j]);
if(str[i] == str[j])
dp[i][j] = min(dp[i][j], dp[i+1][j-1]);
}
}
printf("%d\n", dp[0][m-1]);
}
return 0;
}
POJ 3280 Cheapest Palindrome(区间dp)
最新推荐文章于 2021-08-10 23:34:19 发布