POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280
题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
分析:这是一道最长回文串的变形,就是LCS
一串字符要变成回文,对于一个字符来说,删掉它,或者增加对称的一个该字符,都能达到回文的效果,所以是等价的。所以取代价的的时候选择最小的就可以。
至于动态规划方程:令dp[i][j]表示从第 i 个字符到第j个字符变成回文的最小代价,初始为0。接着LCS
dp[i][j] = min(dp[i+1][j]+cost[s[i]-'a'] , dp[i][j-1]+cost[s[j]-'a']) ;
if(s[i]==s[j]) dp[i][j] = min(dp[i+1][j-1],dp[i][j]);
代码如下:
# include<stdio.h>
# include<string.h>
# define maxn
char s[maxn];
int dp[maxn][maxn],cost[maxn]; int min(int a,int b){
return a<b ? a :b;
} int main(){
int n,m,a,b,i,j;
char temp;
while(scanf("%d%d",&n,&m)!=EOF){
memset(dp,,sizeof(dp));
getchar();
scanf("%s",s+);
getchar();
for(i=;i<=n;i++){
scanf("%c %d%d",&temp,&a,&b);
getchar();
cost[temp-'a'] = min(a,b);
}
for(j=;j<=m;j++){
for(i=j+;i>=;i--){
dp[i][j] = min(dp[i+][j]+cost[s[i]-'a'] , dp[i][j-]+cost[s[j]-'a']) ;
if(s[i]==s[j])
dp[i][j] = min(dp[i+][j-],dp[i][j]);
}
}
printf("%d\n",dp[][m]);
}
return ;
}
POJ 3280 Cheapest Palindrome(DP 回文变形)的更多相关文章
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
- POJ 3280 Cheapest Palindrome DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- POJ 3280 - Cheapest Palindrome - [区间DP]
题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...
- (中等) POJ 3280 Cheapest Palindrome,DP。
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
- POJ 3280 Cheapest Palindrome 简单DP
观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...
- POJ 3280 Cheapest Palindrome (DP)
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...
随机推荐
- Leveraging the Power of Asynchrony in ASP.NET [转]
Asynchronous programming has had a lot of attention in the last couple of years and there are two ke ...
- Qt学习之路(1)------Qt常用类用法说明
Qt常用类 向控制台输出文本 第一个例子,我们采用STL的方式: console.cpp #include <iostream> int main() { std::cout <&l ...
- sorts
各种排序算法: #include <stdio.h> #include <string.h> #include <ctype.h> #include <std ...
- Chrome 控制台实用指南【转】
转自伯乐在线. Chrome 控制台实用指南 前言 Chrome浏览器我想是每一个前端er必用工具之一吧,一部分原因是它速度快,体积不大,支持的新特性也比其它浏览器多,还有一部分我想就是因为它的控制台 ...
- [CODEVS3641]上帝选人
题目描述 Description 世界上的人都有智商IQ和情商EQ.我们用两个数字来表示人的智商和情商,数字大就代表其相应智商或情商高.现在你面前有N个人,这N个人的智商和情商均已知,请你选择出尽量多 ...
- C++类实现AVL树
二叉查找树是个好东西,他让查找,插入,删除,这些常用操作变得高效,但是,他是存在问题的,那就是,在坏的输入序列下,树会退化成链表,这就很尴尬了,于是为了避免这种情况的发生,我们需要一种数据结构,可以自 ...
- 一个PHP书单 -摘自网络
# PHP <PHP程序设计>(第2版) –PHP语法和入门最好的书 <PHP5权威编程> –PHP入门后升级书 <深入PHP:面向对象.模式与实践>(第3版) – ...
- 写一段方便的SQL 循环查每一天的数据
declare @recd int,@i int,@a int,@count int,@day1 date,@day2 date,@days int set @day1='2014-8-24' set ...
- Ubuntu14.04搭建cocos2dx2.2.5开发环境(超级具体)
一 下载解压 官方下载地址:http://www.cocos2d-x.org/download 下载下来之后解压完毕之后会得到一个文件夹cocos2d-x-2.2.5 二 编译 1 安装依赖 cd到c ...
- [Node.js] Node.js Buffers
>> node >>fs.readFile('finnish.txt', function(err,data){ console.log(data); }); // Outpu ...