POJ - 3280

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

id=16272" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; border:1px solid rgb(211,211,211); color:blue; font-size:12px!important">Submit Status

Description

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are currently a single
string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).

Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is "abcba" would read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two
different IDs ("abcb" and "bcba").

FJ would like to change the cows's ID tags so they read the same no matter which direction the cow walks by. For example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards).
Some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the begining to yield the ID "bcbabcb" or removing the letter "a" to yield the ID "bcb". One can add or remove characters at any location in the string yielding
a string longer or shorter than the original string.

Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow's ID tag and the cost of
inserting or deleting each of the alphabet's characters, find the minimum cost to change the ID tag so it satisfies FJ's requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated
costs can be added to a string.

Input

Line 1: Two space-separated integers: N and M

Line 2: This line contains exactly M characters which constitute the initial ID string 

Lines 3.. N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.

Output

Line 1: A single line with a single integer that is the minimum cost to change the given name tag.

Sample Input

  1. 3 4
  2. abcb
  3. a 1000 1100
  4. b 350 700
  5. c 200 800

Sample Output

  1. 900

Hint

If we insert an "a" on the end to get "abcba", the cost would be 1000. If we delete the "a" on the beginning to get "bcb", the cost would be 1100. If we insert "bcb" at the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.
这道题目我做的时候,真的纠结了半天。想用区间dp吧,可是又怕超内存。可是后来思考发现

好像题目是2000,我理解为了20000,结果一阵哭,可是做题目的时候又遇到麻烦事情了,就是区间

有点搞懵了dp[i][j]代表着[j,i]看着就有点蛋疼,搞了好久才搞定

所以在此提示读者,认真看题。否则后果自负啊

解说一下代码:

当中dp[i][j]代表着[i,j]这个区间变成回文字符串的最小代价,(本来是[j,i],后来为了大家的方便我帮她改了,让大家可以更好的理解)

dp[i][j]=min(dp[i][j-1]+cost[str[j]-'a'],dp[i+1][j]+cost[str[i]-'a']);

这个的意思是区间dp[i,j]是由dp[i][j-1]或者是dp[i+1][j]变过来的

接着是cost数组的处理 cost[op[0]-'a']=min(a,b);

由于要使代价最小的话,那么当我们面对一个字符的时候是应该删掉它还是应该添加与其位置相应的字符呢

那就要看代价了,所以这里有个小技巧。就是将操作隐藏,无论是你是删掉还是添加,肯定是选择代价小的那个操作

那么我之前取删除与添加的最小值就可以

然后是if(str[i]==str[j])dp[i][j]=dp[i+1][j-1];假设str[i]==str[j]。那么开头和结尾相等。证明不用进行操作了

由于他们已经匹配成功了我们要做的就是将他从状态中递推过来dp[i][j]=dp[i+1][j-1]+0,就能够了。
  1. /*
  2. Author: 2486
  3. Memory: 16120 KB Time: 16 MS
  4. Language: G++ Result: Accepted
  5. */
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <algorithm>
  9. using namespace std;
  10. const int maxn=2000+5;
  11. int dp[maxn][maxn];
  12. char str[maxn],op[10];
  13. int cost[30];
  14. int n,m,a,b;
  15. int main() {
  16. while(~scanf("%d%d",&n,&m)) {
  17. scanf("%s",str+1);
  18. for(int i=0; i<n; i++) {
  19. scanf("%s%d%d",op,&a,&b);
  20. cost[op[0]-'a']=min(a,b);
  21. }
  22. memset(dp,0,sizeof(dp));//当中dp[i][j]代表着[i,j]这个区间
  23. for(int j=2; j<=m; j++) {
  24. for(int i=j-1; i>0; i--) {
  25. if(str[i]==str[j])dp[i][j]=dp[i+1][j-1];
  26. else dp[i][j]=min(dp[i][j-1]+cost[str[j]-'a'],dp[i+1][j]+cost[str[i]-'a']);
  27. }
  28. }
  29. printf("%d\n",dp[1][m]);
  30. }
  31. return 0;
  32. }

POJ - 3280Cheapest Palindrome-经典区间DP的更多相关文章

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

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

  2. POJ 1160 经典区间dp/四边形优化

    链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...

  3. Cheapest Palindrome(区间DP)

    个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...

  4. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  5. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  7. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  8. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

  9. POJ 2176 Folding(区间DP)

    题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...

随机推荐

  1. 第一课trie 树 POJ 2001

    最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...

  2. C - Between the Offices

    Problem description As you may know, MemSQL has American offices in both San Francisco and Seattle. ...

  3. android开发 设备调试问题

    如果在mac下面用趁机开发android程序,发现不能连接上时就需要进行简单设置即可 先设置Finder为可以显示所有隐藏文件夹: 打开终端,输入如下命令: 显示Mac隐藏文件的命令:defaults ...

  4. Spring + Redis ( 简单使用)

    1.Redis 的 Java API Java 中 使用 Redis 工具,要先去 maven 仓库中,下载 jedis jar包 jedis 依赖 <dependency> <gr ...

  5. jquery中的left和top

    left 和 top /*1. 获取元素基于定位容器的位置*/ /*返回的是对象 属性 left top */ var position = $('.inner').position(); conso ...

  6. CSS样式优先级和权重问题(部分)

    内联样式: <div style="font-size: 12px;">姓名</div> 外部样式: <link rel="styleshe ...

  7. Android开放百度地图集成

    1.创建应用 获取AK (我理解为Application key)  通过百度账号登录百度地图开放平台,进入API控制台 http://lbsyun.baidu.com/apiconsole/key ...

  8. os的进程调度算法(抄袭的)

    package me.letterwish.test; import java.io.BufferedInputStream; import java.io.FileInputStream; impo ...

  9. 浏览器内置的base64方法

    Base64是一种基于64个可打印字符来表示二进制数据的表示方法.在Base64中的可打印字符包括字母A-Z.a-z.数字0-9,这样共有62个字符,此外两个可打印符号在不同的系统中而不同(维基百科: ...

  10. 原来这才是Kafka的“真面目”

    作者介绍 郑杰文,腾讯云存储,高级后台工程师,2014 年毕业加入腾讯,先后从事增值业务开发.腾讯云存储开发.对业务性.技术平台型后台架构设计都有深入的探索实践.对架构的海量并发.高可用.可扩展性都有 ...