POJ3280 简单DP

poj3280:[url]http://poj.org/problem?id=3280[/url]
[size=small]简单的多子问题多选择dp问题:
dp[i][j]表示字符下标为i到j的子字符串构成回文的最小消费,构成解的子问题有以下两种情况:
1、str[i]!=str[j],则最小费用为dp[i][j]=min{dp[i+1][j]+cost(str[i]),dp[i][j-1]+cost[str[j]]},其中cost(char),是取得增加和删除该字符的费用较小值;
2、str[i]==str[j],则最小费用为dp[i][j]=min{dp[i][j],dp[i+1][j-1]},保证此时dp[i][j]已经求出。[/size]

#include <iostream>
#include <fstream>
#define MAX_DP 2011

using namespace std;

char str[MAX_DP];
unsigned int dp[MAX_DP][MAX_DP];
int cost[26];

unsigned int min(unsigned int a,unsigned int b)
{
if(a>=b)
return b;
else
return a;
}

unsigned int dodp(unsigned int start,unsigned int end)
{
if(start>=end)
return 0;
if(dp[start][end])
return dp[start][end];

dp[start][end]=min(dodp(start+1,end)+cost[str[start]-'a'],dodp(start,end-1)+cost[str[end]-'a']);
if(str[start]==str[end])
dp[start][end]=min(dp[start][end],dodp(start+1,end-1));
return dp[start][end];
}


int main()
{

unsigned int m,n;

memset(cost,0,sizeof(cost));
memset(dp,0,sizeof(dp));

cin>>n>>m;
cin>>str;

for(int i=0;i<n;i++)
{
char temp;
unsigned int a,b;
cin>>temp;
cin>>a>>b;
cost[temp-'a']=min(a,b);
}

cout<<dodp(0,m-1)<<endl;

return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值