博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3280(区间dp)
阅读量:7254 次
发布时间:2019-06-29

本文共 1346 字,大约阅读时间需要 4 分钟。

 

题目连接:

题意:给定一个长度为m(m<=2000)的小写字母字符串,在给定组成该字符串的n(n<=26)个字符的添加和删除费用,求使原字符串变为回文串的最小费用。

分析:首先明确,删除一个字母和增加一个字母是等价的,如果删除一个字符一个字符使得原字符串变成回文,那么必定可以增加一个字符使原字符串变成回文,因此对于删除和增加操作去费用最少的即可。

dp[i][j]表示区间i~j形成回文串最少费用,则:

dp[i][j] = min(dp[i+1][j]+cost[str[i]-'a'], dp[i][j-1]+cost[str[j]-'a']);

 if(str[i] == str[j])dp[i][j] = min(dp[i][j],dp[i+1][j-1]);

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long#define mod 100000000#define inf 0x3f3f3f3f#define eps 1e-9#define N 100010#define FILL(a,b) (memset(a,b,sizeof(a)))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int cost[30],dp[2110][2110];char s[10],str[2100];int dfs(int l,int r){ if(dp[l][r]!=-1)return dp[l][r]; if(l>=r)return 0; int temp=inf; if(str[l]==str[r]) temp=min(temp,dfs(l+1,r-1)); temp=min(temp,dfs(l+1,r)+cost[str[l]-'a']); temp=min(temp,dfs(l,r-1)+cost[str[r]-'a']); return dp[l][r]=temp;}int main(){ int n,m; while(scanf("%d%d",&n,&m)>0) { scanf("%s",str+1); for(int i=1;i<=n;i++) { int add,det; scanf("%s%d%d",s,&add,&det); cost[s[0]-'a']=min(add,det); } FILL(dp,-1); printf("%d\n",dfs(1,m)); }}
View Code

 

转载于:https://www.cnblogs.com/lienus/p/4266498.html

你可能感兴趣的文章
Linux Process VS Thread VS LWP
查看>>
杭电多校第二场 1005 hack it
查看>>
VMM2012应用指南之9-向VMM中添加VMware ESX Server主机
查看>>
Exchange日常管理之二十二:配置保留策略
查看>>
Android -- Webview自适应屏幕
查看>>
从Delphi 7升级到Delphi XE
查看>>
android圆形图像
查看>>
Oracle数据库备份还原工具之Expdp/IMPdp
查看>>
【转】android JNI编程 一些技巧(整理)
查看>>
MySQL之终端(Terminal)管理数据库、数据表、数据的基本操作
查看>>
各种排序算法汇总
查看>>
C#巧用Excel模版变成把Table打印出来
查看>>
SOAP 及其安全控制--转载
查看>>
JarSearch
查看>>
[Unity3D][Vuforia][IOS]vuforia在unity3d中添加自己的动态模型,识别自己的图片,添加GUI,播放视频...
查看>>
Freemodbus介绍及测试
查看>>
[转]Phantomjs实现获取网页快照并生成缩略图
查看>>
leveldb源码学习系列
查看>>
Linux 运行 apt-get install 就出现jdk installer 错误的解决方法
查看>>
Android OpenGL ES(九)绘制线段Line Segment .
查看>>