poj3280(区间dp)
题目连接:http://poj.org/problem?id=3280
题意:给定一个长度为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 <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#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|1
using namespace std;
int cost[],dp[][];
char s[],str[];
int dfs(int l,int r)
{
if(dp[l][r]!=-)return dp[l][r];
if(l>=r)return ;
int temp=inf;
if(str[l]==str[r])
temp=min(temp,dfs(l+,r-));
temp=min(temp,dfs(l+,r)+cost[str[l]-'a']);
temp=min(temp,dfs(l,r-)+cost[str[r]-'a']);
return dp[l][r]=temp;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)>)
{
scanf("%s",str+);
for(int i=;i<=n;i++)
{
int add,det;
scanf("%s%d%d",s,&add,&det);
cost[s[]-'a']=min(add,det);
}
FILL(dp,-);
printf("%d\n",dfs(,m)); }
}
poj3280(区间dp)的更多相关文章
- Cheapest Palindrome [POJ3280] [区间DP] [经典]
一句话题意:每个字母添加和删除都相应代价(可以任意位置 增加/删除),求把原串变成回文串的最小代价 Description 保持对所有奶牛的跟踪是一项棘手的任务,因此农场主约翰已经安装了一个系统来实现 ...
- poj3280 区间dp
//Accepted 15880 KB 250 ms #include <cstdio> #include <cstring> #include <iostream> ...
- [poj3280]Cheapest Palindrome_区间dp
Cheapest Palindrome poj-3280 题目大意:给出一个字符串,以及每种字符的加入代价和删除代价,求将这个字符串通过删减元素变成回文字符串的最小代价. 注释:每种字符都是小写英文字 ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- 区间DP的摸索
(poj真的炸了,以下代码可能有误) 按照下面这个做题顺序,对区间DP不再那么迷了 LOJ1422 是 dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])而不是d ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
随机推荐
- UVA 2039 Pets(网络流)
Problem Description Are you interested in pets? There is a very famous pets shop in the center of th ...
- office文档转pdf
这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- QT对话框中show和exec的区别
转自:http://hi.baidu.com/wangjuns8/blog/item/24b382460dd1c1338694737d.html QDialog的显示有两个函数show()和exec( ...
- 使用awk和grep做简单的统计
grep 或 egrep 或awk 过滤两个或多个关键词: grep -E ‘123|abc’ filename // 找出文件(filename)中包含123或者包含abc的行 egrep ‘123 ...
- 基于visual Studio2013解决面试题之0609寻找链表公共节点
题目
- 也谈基于NodeJS的全栈式开发(基于NodeJS的前后端分离)
前言 为了解决传统Web开发模式带来的各种问题,我们进行了许多尝试,但由于前/后端的物理鸿沟,尝试的方案都大同小异.痛定思痛,今天我们重新思考了“前后端”的定义,引入前端同学都熟悉的NodeJS,试图 ...
- 一个能够自己主动生成静态库,自己主动安装程序的Makefile
.PHONY:clean install CC=g++ CFLAGS=-Wall -g BIN=libecho.a INCLUDE=echo SRC=src OBJS=Socket.o Rio.o T ...
- 百度地图SDK for Android v2.1.2全新发布
2013年6月20日 Android SDK:V2.1.2产品上线 新增: 自定义指南针位置(类:MapController,方法:setCompassMargin) 自定义当前位置图标(类:MyLo ...
- DRP分销系统总结
上个月看完的分销系统的视频,用了漫长的时间看这个项目视频,能安慰自己的是不光是看视频了,还做了很多自己想做的事情,比如驾照拿下来了,比如参加了一些考试,比如讲了一些课程等等.把这个系统的总结总算是补上 ...
- greatis很不错,出售源代码
http://www.greatis.com/delphicb/ 特别是: http://www.greatis.com/delphicb/imgedit/