题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价为多少。

解法:设d[i][j]表示将字符串中从第i位至第j位变成回文串所需要的代价。若s[i] == s[j],d[i][j] = d[i+1][j-1];否则的话,有四种处理方法。

   对xa.......by,可以将其变为xa......b,yxa.....by,a.......by,xa.....byx中的任意一种再处理。所以d[i][j] = min(d[i][j-1] + c2[s[j]], d[i][j-1] + c1[s[j]], d[i+1][j] + c2[s[i]], d[i+1][j] + c1[s[i]])。化简为d[i][j] = min(d[i][j-1] + min(c1[s[j]], c2[s[j]]), d[i+1][j] + min(c1[s[i]], c2[s[i]]))。(此公式看着没有化简,但实际上可以省很多代码,我的代码改了以后少了近200b)

tag:字符串,回文串,dp

 /*
* Author: Plumrain
* Created Time: 2013-11-17 21:44
* File Name: DP-POJ-3280.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
const int maxint = ; char s[];
int d[][];
map<char, int > mp; int main()
{
int n, len;
while (scanf ("%d%d", &n, &len) != EOF){
scanf ("%s", s);
char x;
int t1, t2;
mp.clear();
for (int i = ; i < n; ++ i){
x = 'P';
while (!(x >= 'a' && x <= 'z')) scanf ("%c", &x);
scanf ("%d%d", &t1, &t2);
mp[x] = min(t1, t2);
} CLR (d);
for (int i = ; i < len; ++ i)
d[i][i] = ;
for (int i = len-; i >= ; -- i)
for (int j = i+; j < len; ++ j){
if (s[i] == s[j]) d[i][j] = d[i+][j-];
else
d[i][j] = min(maxint, min(d[i+][j] + mp[s[i]], d[i][j-] + mp[s[j]]));
}
printf ("%d\n", d[][len-]);
}
return ;
}

POJ 3280 Cheapest Palindrome(水题)的更多相关文章

  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

    链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...

  3. POJ 3280 Cheapest Palindrome(DP 回文变形)

    题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...

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

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

  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求改成回文串的最小花费)

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

  7. POJ 3280 Cheapest Palindrome(DP)

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

  8. POJ 3280 Cheapest Palindrome (区间DP) 经典

    <题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...

  9. POJ 3280 Cheapest Palindrome DP题解

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

随机推荐

  1. Java环境的安装与配置

    Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...

  2. IOS开发效率之为Xcode添加常用的代码片段

    IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...

  3. Vijos1734 NOI2010 海拔 平面图最小割

    建立平面图的对偶图,把最小割转化成最短路问题 Dijkstra算法堆优化 (被输入顺序搞WA了好几次T_T) #include <cstdio> #include <cstring& ...

  4. javascript——函数属性和方法

    <script type="text/javascript"> //每个函数都包含两个属性:length 和 prototype //length:当前函数希望接受的命 ...

  5. Linq查询IEnumerable与IQueryable

    class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch stp = new Stopwatch() ...

  6. python split()黑魔法

    split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...

  7. WordPress批量修改文章内容、URL链接、文章摘要

    通过SQL语句来批量修改wordpress博客内容,文章中所有语句都使用默认的wp_表前缀,如果您的数据表前缀不是wp_则需要在语句中作相应更改. 方法/步骤   批量修改文章内容 如果您想替换之前写 ...

  8. JS和jQuery获取节点的兄弟,父级,子级元素

    原文转自http://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的 ...

  9. SharePoint 2013 如何使用TaxonomyWebTaggingControl 控件

    在该文章中,我将介绍如何使用TaxonomyWebTaggingControl控件, 首先我相信您已经在SharePoint Managed Metadata Service里定义Term Sets, ...

  10. 转:mysql性能优化的19个要点

    原文来自于:http://outofmemory.cn/mysql/mysql-performance-tips 1.为查询优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方 ...