poj 3280 简单dp
来源:互联网 发布:silk mobile python 编辑:程序博客网 时间:2024/05/17 09:45
实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称。
故两种操作看成一种,只需要保留花费少的那个即可
然后
令
dp[i][j]表示从位置i到j的子串转化为回文串需要的次数
若 s[i]== s[j] 则dp[i][j] = dp[i + 1][j - 1]
否则 dp[i][j] = min(dp[i+1][j] + cost[i], dp[i][j - 1] + cost[j])
代码如下:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define MAX 2111using namespace std;int dp[MAX][MAX];int cost[300];char ch[MAX];int N,M;int DP(){ int i,j,k,r; for(i=0;i<M;i++) dp[i][i]=0; for(r=2;r<=M;r++) //子段的长度 for(i=0;i<=M-r;i++) //i为首指针,j为尾指针。 { j=i+r-1; if(ch[i]==ch[j]) dp[i][j]=dp[i+1][j-1]; else { dp[i][j]=min(dp[i+1][j]+cost[ch[i]],dp[i][j-1]+cost[ch[j]]); } } return dp[0][M-1]; //最优解}int main(){ int i,j; char c; int ins,del; scanf("%d%d",&N,&M); getchar(); gets(ch); for(i=0;i<N;i++) { scanf("%c%d%d",&c,&ins,&del); getchar(); cost[c]=min(ins,del); } int ans=DP(); printf("%d\n",ans); return 0;}
0 0
- poj 3280 简单dp
- POJ 3280 简单区间 DP
- poj 3280(简单区间dp)
- POJ 3280 Cheapest Palindrome 简单DP
- POJ 2181简单DP
- POJ-2004 简单DP..
- poj 2385 简单dp
- poj 3486 简单dp
- poj 2353 简单dp
- poj-2355-简单dp
- poj-2479 简单DP
- poj 3267 简单dp
- poj 3132简单dp
- poj 2948 简单dp
- POJ 3176 简单DP
- POJ 3176 简单DP
- poj 3616(简单dp)
- poj 3920(简单dp)
- [Phonegap+Sencha Touch] 移动开发8、store和proxy
- 某游戏公司面试全过程
- LeetCode-Path Sum II
- [Phonegap+Sencha Touch] 移动开发4、Sencha Touch初步使用
- 安装滚轮插件
- poj 3280 简单dp
- [Phonegap+Sencha Touch] 移动开发9、scss编译成css,如何去除-moz-、-o-、-ms-前缀
- 树状数组
- web 项目连接mysql报NullPointerException问题
- 关于局部特征介绍
- Sniffer
- [Phonegap+Sencha Touch] 移动开发10 调用focus方法使输入框获得焦点,不弹出软键盘的解决方法
- [Phonegap+Sencha Touch] 移动开发11 移动平台的link标签和meta标签——神奇的功效
- 小俞同学的数表