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)). ...
随机推荐
- IBMWebsphere 使用jar包删除文件
1. 先使用ant打包一个jar包,删除其他不要的目录和文件,仅保留一个空的xxx.war文件夹("xxx"对应was上的工程安装根目录) 2. 在文件夹下新建一个META-INF ...
- Application windows are expected to have a root view controller at the end of application launch
今天把Xcode升级了,模拟器 用的12.1的系统,运行时发现项目总是崩溃,采用9.3系统的测试机发现错误日志如下: Application windows are expected to have ...
- java编译器和java解释器、JVM
作用: JVM:JVM有自己完善的硬件架构,如处理器.堆栈(Stack).寄存器等,还具有相应的指令系统(字节码就是一种指令格式).JVM屏蔽了与具体操作系统平台相关的信息,使得Java程序只需要生成 ...
- (转)vuex2.0 基本使用(2) --- mutation 和 action
我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...
- Linux 与 Windows 文件互传(VMWare)
虚拟机无桌面的Linux 与 物理机Windows 文件互传有很多种方法,现在先说一种通过共享文件夹的形式,其他方法后续再补充 1. 背景 1) 虚拟机系统:VMWare无桌面的 ...
- JQuery特效之心形图片墙
效果如图: 代码如下: <!doctype html> <html lang="en"> <head> <meta charset=&qu ...
- SQL Server之十大存储过程
下面介绍十大不同类型存储过程. 用户自定义存储过程 . 创建语法 create proc | procedure pro_name [{@参数数据类型} [=默认值] [output], {@参数数据 ...
- mqtt-client回调方法简介
mqtt-client回调方法简介 毫无疑问Callback方式是最复杂的一种,但是其也是能够提供更好的服务,因此有必要好好研究,下面就是对使用回调方式的简单介绍: 一.在使用回调方式前,先通过MQT ...
- 【sqli-labs】 less32 GET- Bypass custom filter adding slashes to dangrous chars (GET型转义了'/"字符的宽字节注入)
转义函数,针对以下字符,这样就无法闭合引号,导致无法注入 ' --> \' " --> \" \ --> \\ 但是,当MySQL的客户端字符集为gbk时,就可能 ...
- C# 去掉代码前边空格(格式化代码)
private void button1_Click(object sender, EventArgs e) { textBox2.Text = ""; string str = ...