题目链接: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 回文变形)的更多相关文章

  1. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  2. POJ 3280 Cheapest Palindrome(DP)

    题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...

  3. POJ 3280 Cheapest Palindrome DP题解

    看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...

  4. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  5. POJ 3280 - Cheapest Palindrome - [区间DP]

    题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...

  6. (中等) POJ 3280 Cheapest Palindrome,DP。

    Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...

  7. POJ 3280 Cheapest Palindrome(DP)

    题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...

  8. POJ 3280 Cheapest Palindrome 简单DP

    观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...

  9. POJ 3280 Cheapest Palindrome (DP)

     Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...

随机推荐

  1. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  2. CLR Profiler 性能分析工具 (转)

    原文地址:http://www.cnblogs.com/kevinlzf/archive/2010/11/12/1876066.html 下载地址:http://www.microsoft.com/e ...

  3. SRM 442(1-250pt, 1-500pt)

    DIV1 250pt 题意:将一个数表示成质因子相乘的形式,若乘式所含数字的个数为质数,则称A为underprime.比如12 = 2*2*3,则含3个数字,是underprime.求A, B之间un ...

  4. freemarke之TemplateDirectiveModel详解

    http://hougbin.iteye.com/blog/1457924 TemplateDirectiveModel接口是freemarker自定标签或者自定义指令的核心处理接口.通过实现该接口, ...

  5. Lamda和Linq语法对比详细

    本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...

  6. 【转】Android 快捷方式的创建

    http://blog.csdn.net/lenmoyouzi/article/details/16939977 一.在日常开发中,我们经常会遇到这样的需求就是网桌面添加快捷方式:常见的快捷方式有两种 ...

  7. [Redux] Colocating Selectors with Reducers

    We will learn how to encapsulate the knowledge about the state shape in the reducer files, so that t ...

  8. [Javascript + rxjs] Introducing the Observable

    In this lesson we will get introduced to the Observable type. An Observable is a collection that arr ...

  9. PureMVC(JS版)源码解析(九):View类

    在讲解View类之前,我们先回顾一下PureMVC的模块划分:      在PureMVC中M.V.C三部分由三个单例类管理,分别是Model/View/Controller.PureMVC中另外一个 ...

  10. Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

    Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...