POJ 3280 —— Cheapest Palindrome

题目:http://poj.org/problem?id=3280

经典的区间DP

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

char s[2005], ch[2];
int cost[26];
int dp[2005][2005];

int main ()
{
    int n, m, c1, c2;
    scanf("%d%d%s", &n, &m, s);
    for(int i=0; i<n; i++) {
        scanf("%s%d%d", ch, &c1, &c2);
        cost[ch[0]-'a'] = min(c1, c2);
    }
    int len = strlen(s);
    for(int step=1; step<len; step++) {
        for(int i=0; i+step<len; i++) {
            int j = i+step;
            if(s[i]==s[j])    dp[i][j] = dp[i+1][j-1];
            else    dp[i][j] = min(dp[i+1][j]+cost[s[i]-'a'], dp[i][j-1]+cost[s[j]-'a']);
        }
    }
    printf("%d", dp[0][len-1]);
    return 0;
}

 

posted on 2016-03-30 22:52  SuperChan  阅读(136)  评论(0)    收藏  举报

导航