poj 3280 Cheapest Palindrome

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

题目分类:动态规划

代码

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int dp[2002][2002];

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    char s[2002];
    cin>>s;
    int cost[200];
    int a, b;
    char c;
    for(int i=1;i<=n;i++)
    {
        cin>>c>>a>>b;
        cost[c] = a > b ? b : a;   // 插入a和删除a其实是等价,只需要选择出来一个较小的即可
    }
    for(int i=m-1;i>=0;i--)      //从后面往前
    {
        for(int j=i+1;j<m;j++)
        {
            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]], dp[i][j-1]+cost[s[j]]);
        }
    }
    cout<<dp[0][m-1]<<endl;
    return 0;
}

 

posted @ 2015-11-03 22:37  Gssol  阅读(116)  评论(0编辑  收藏  举报