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.
 
原创博客:戳这里

题目大意:给定一个字符串S及其长度M与S所含有的字符种数N(最多26种小写字母),然后给定这N种字母Add与Delete的代价,求将S变为回文串的最小代价和。

分析:

这题算得上是一个很经典的模型了。先来说说最原始的模型,通过增添或者删除某些字母将一个字符串变为回文串,求最少增添或者删除次数,其实在这里增添和删除是没有区别的,我们只考虑删除就行了,对于这个模型通常有2种解法:

①将S反转得到S',然后求两者最长公共子串的长度L,再用S的长度减去L便是答案了

②用 dp[i][j] 表示 [i,j] 区间的串的最优解,

若s[i]==s[j],则dp[i][j]=dp[i+1][j-1](请思考为什么)

否则 dp[i][j] = min (d[i+1][j] , dp[i][j-1]) + 1,因为s[i]!=s[j],所以必定要删去左右边界的某一个才能变成一个回文串,至于到底删去哪一个,当然是取 dp[i+1][j] 和 dp[i][j-1] 的MIN了

然后反观这个题,只是在add和delete的操作上附加了一个代价,所以这里的add和delete便不再等价了(当然是在add!=delete时),于是对于 [i+1,j] 和 [i,j] 之间的转换,当然要选择是在 [i+1,j] 上add一个字母变为 [i,j] 还是 在[i,j] 上delete一个字母变为 [i+1,j] 中的最优解了。

所以第一种方法显然不再适用,对于第二种方法,只要稍微改一下就行了

若s[i]==s[j],则dp[i][j]=dp[i+1][j-1]仍然成立

否则 dp[i][j] = min  (   d[i+1][j] + min(add[s[i]],delete[s[i]])  ,   dp[i][j-1]  + min(add[s[j]],delete[s[j]]) )   注意写代码时别用min嵌套,会出错!

  1. 1 #include <iostream>
  2. 2 #include <cstring>
  3. 3 #include <map>
  4. 4 #include <algorithm>
  5. 5 using namespace std;
  6. 6 typedef long long ll;
  7. 7 const int maxn = 2 * 1e3 + 10;
  8. 8 const int maxx = 1e6 + 10;
  9. 9 int dp[maxn][maxn];
  10. 10 string sa;
  11. 11 map<char, int> w;
  12. 12 int main()
  13. 13 {
  14. 14 int n, m;
  15. 15 cin >> n >> m;
  16. 16 cin >> sa;
  17. 17 char u;
  18. 18 int x, y;
  19. 19 for(int i = 0; i < n; ++i)
  20. 20 {
  21. 21 cin >> u >> x >> y;
  22. 22 w[u] = min(x, y);
  23. 23 }
  24. 24 // cout<<w[sa[0]]<<endl;
  25. 25 for(int i = 1; i < m; ++i) //区间长度为i+1
  26. 26 {
  27. 27 for(int j = 0; i + j < m; ++j)
  28. 28 {
  29. 29 if(sa[j] == sa[i + j])
  30. 30 dp[j][i + j] = dp[j + 1][i + j - 1];
  31. 31 else
  32. 32 {
  33. 33 dp[j][i + j] = min(dp[j + 1][i + j] + w[sa[j]], dp[j][i + j - 1] + w[sa[i + j]]);
  34. 34
  35. 35 }
  36. 36 }
  37. 37 }
  38. 38 cout<< dp[0][m - 1] << endl;
  39. 39 return 0;
  40. 40 }

POJ - 3280 Cheapest Palindrome 【区间dp】【非原创】的更多相关文章

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

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

  2. POJ 3280 Cheapest Palindrome ( 区间DP && 经典模型 )

    题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价 ...

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

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

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

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

  5. POJ 3280 Cheapest Palindrome (DP)

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

  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)

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

  9. POJ 3280 Cheapest Palindrome 简单DP

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

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

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

随机推荐

  1. 编年史:OI算法总结

    目录(按字典序) A --A* D --DFS找环 J --基环树 S --数位动规 --树形动规 T --Tarjan(e-DCC) --Tarjan(LCA) --Tarjan(SCC) --Ta ...

  2. [Usaco2008 Nov]Buying Hay 购买干草

    题目描述 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅 ...

  3. Docker下梦织CMS的部署

    摘要:Docker的广泛应用相对于传统的虚拟机而言提高了资源的利用率,推广后docker的影响不容忽视,在启动速度.硬盘.内存.运行密度.性能.隔离性和迁移性方面都有很大的提高.本次实训我们在cent ...

  4. DSL是什么?Elasticsearch的Query DSL又是什么?

    1.DSL简介 DSL 其实是 Domain Specific Language 的缩写,中文翻译为领域特定语言.而与 DSL 相对的就是 GPL,这里的 GPL 并不是我们知道的开源许可证(备注:G ...

  5. 深圳某小公司面试题:AQS是什么?公平锁和非公平锁?ReentrantLock?

    AQS总体来说没有想象中那么难,只要了解它的实现框架,那理解起来就不是什么问题了. AQS在Java还是占很重要的地位的,面试也是经常会问. 目前已经连载11篇啦!进度是一周更新两篇,欢迎持续关注 [ ...

  6. 死锁案例 GAP 锁 没有就插入,存在就更新

    https://mp.weixin.qq.com/s/2obpN57D8hyorCMnIu_YAg 死锁案例八 文 | 杨一 on 运维 转 | 来源:公众号yangyidba 一.前言 死锁其实是一 ...

  7. 服务之间的调用为啥不直接用 HTTP 而用 RPC?

    什么是 RPC?RPC原理是什么? 什么是 RPC? RPC(Remote Procedure Call)-远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.比 ...

  8. 使用jiffies的时间比较函数time_after、time_before

    1. jiffies简介 首先,操作系统有个系统专用定时器(system timer),俗称滴答定时器,或者系统心跳. 全局变量jiffies取值为自操作系统启动以来的时钟滴答的数目,在头文件< ...

  9. hashlib,configparser,logging

    # hash: 算法, 结果是什么? 是内存地址, # print(hash('123')) # dic = {'name':'alex'} # print(hash('name')) # print ...

  10. 一文打尽端口复用 VS Haproxy端口复用

    出品|MS08067实验室(www.ms08067.com) 本文作者:Spark(Ms08067内网安全小组成员) 1.概述   Haproxy是一个使用c语言开发的高性能负载均衡代理软件,提供tc ...