POJ - 3280Cheapest Palindrome-经典区间DP
POJ - 3280
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 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 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 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). 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 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 3 4 Sample Output 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,就能够了。 /* |
POJ - 3280Cheapest Palindrome-经典区间DP的更多相关文章
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- POJ 1160 经典区间dp/四边形优化
链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...
- Cheapest Palindrome(区间DP)
个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...
- HDU4632:Palindrome subsequence(区间DP)
Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 2955 括号匹配 区间dp
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6033 Accepted: 3220 Descript ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- HDU 4632 Palindrome subsequence (区间DP)
题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...
- POJ 2176 Folding(区间DP)
题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...
随机推荐
- C#Cookie操作类,删除Cookie,给Cookie赋值
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...
- mysql数据库知识点总结
一.数据库的基本操作 --------------------------------------------------------------数据库的安装以后更新----------------- ...
- Eclipse中配置SVN(步骤简述)
————Eclipse中配置SVN(步骤简述)———— 1.有客户端(tortoiseSVN),服务器端(visualSVN) 两种,根据需要安装,安装后需重启电脑 2.服务器端配置:创建版本库(放工 ...
- SAP computer之RAM
RAM The RAM is a 16 X 8 static TTL RAM. We can program the RAM by means of the address and data swit ...
- python tips:类的专有属性
实例通常能够调用类的属性,但是有些属性是类专有的,实例无法调用. 实例调用方法时查找属性时,首先在自己的__dict__中找,找不到去类中找,在类中能够找到的属性都位于dir(cls)中,如果类的某些 ...
- CF319E Ping-Pong 线段树 + vector + 思维
Code: #include<bits/stdc++.h> #define N 3000009 #define maxn 3000009 #define ll long long #def ...
- git 还原到某次commit
不可逆提交 一,reset 1.git log查看提交记录 git log 2.选择某次提交的commit ID,ctrl+c复制提交ID 3.使用git reset –hard 还原到某一次提交 g ...
- [bzoj1860 ZJOI2006] 超级麻将 (线性dp)
传送门 Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的 ...
- IDEA返回上一步
在开发中进入一个方法后想要到原来那行 ctrl+alt+左 回到上一步 ctrl+alt+右 回到下一步
- autoware安装
1.Autoware的地址为https://github.com/CPFL/Autoware2.Install dependencies for Ubuntu 16.04 kinetic安装教程ins ...