题意:给定一个完全由小写字母组成的字符串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

  1. /*
  2. * Author: Plumrain
  3. * Created Time: 2013-11-17 21:44
  4. * File Name: DP-POJ-3280.cpp
  5. */
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <map>
  10. #include <utility>
  11.  
  12. using namespace std;
  13.  
  14. #define CLR(x) memset(x, 0, sizeof(x))
  15. const int maxint = ;
  16.  
  17. char s[];
  18. int d[][];
  19. map<char, int > mp;
  20.  
  21. int main()
  22. {
  23. int n, len;
  24. while (scanf ("%d%d", &n, &len) != EOF){
  25. scanf ("%s", s);
  26. char x;
  27. int t1, t2;
  28. mp.clear();
  29. for (int i = ; i < n; ++ i){
  30. x = 'P';
  31. while (!(x >= 'a' && x <= 'z')) scanf ("%c", &x);
  32. scanf ("%d%d", &t1, &t2);
  33. mp[x] = min(t1, t2);
  34. }
  35.  
  36. CLR (d);
  37. for (int i = ; i < len; ++ i)
  38. d[i][i] = ;
  39. for (int i = len-; i >= ; -- i)
  40. for (int j = i+; j < len; ++ j){
  41. if (s[i] == s[j]) d[i][j] = d[i+][j-];
  42. else
  43. d[i][j] = min(maxint, min(d[i+][j] + mp[s[i]], d[i][j-] + mp[s[j]]));
  44. }
  45. printf ("%d\n", d[][len-]);
  46. }
  47. return ;
  48. }

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. Android ListView 嵌套 ImageView,如何响应ImageView的点击和长按事件

    http://www.tuicool.com/articles/EZv2Uv 1.先说下嵌套在ListView中的ImageView如何响应点击事件 方法:在imageView中设置onClick属性 ...

  2. WCF,WebAPI,WCFREST和WebService的区别

    Web ServiceIt is based on SOAP and return data in XML form.It support only HTTP protocol.It is not o ...

  3. one way WebService

    WSDL支持4种消息交换方式:   1)单向(One-way):服务端接收消息:   2)请求响应(Request-response):服务端点接收请求消息,然后发送响应消息:   3)要求应答(So ...

  4. 【转】jQuery教程

    “jQuery风暴” 推荐及配套代码下载 ziqiu.zhang 2011-03-24 00:28 阅读:15339 评论:100   从零开始学习jQuery(剧场版) 你必须知道的javascri ...

  5. 安装完 MySQL 后必须调整的 10 项配置

    原文出处: mysqlperformanceblog   译文出处:开源中国   欢迎分享原创到伯乐头条 当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议 ...

  6. ICE学习第四步-----客户端请求服务器返回数据

    这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...

  7. ajax 文件上传,ajax

    ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...

  8. PHP框架_Smarty_实现登录功能

    1.项目框架 |--mvc |--data 数据 |--cache 缓存 |--template_c 模板生成目录 |--framework |--function |--function.php 功 ...

  9. SQL 测试

    1.SQL 指的是? 您的回答:Structured Query Language 2.哪个 SQL 语句用于从数据库中提取数据? 您的回答:SELECT 3.哪条 SQL 语句用于更新数据库中的数据 ...

  10. sdk manager更新失败,显示Download interrupted: read timed out,应该如何解决?

    今天开始第一天学习Android,就遇到一个大问题.sdk manager无法自动更新,总在提示超时!!! 经过网上查询发现问题原因是. 在利用android sdk manager更新时失败,具体情 ...