Cheapest Palindrome(区间DP)
个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的
递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是从把间距为1到ch.size()全部算出来,难道
这就是动态规划,无后效性,即每一步都是最优的状态,所以把所有状况全部解决然后就可以一步一步往后面推了??值得深思
网上题解:
分析:我们知道求添加最少的字母让其回文是经典dp问题,转化成LCS求解。这个是一个很明显的区间dp
我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价。
那么对于dp【i】【j】有三种情况
首先:对于一个串如果s【i】==s【j】,那么dp【i】【j】=dp【i+1】【j-1】
其次:如果dp【i+1】【j】是回文串,那么dp【i】【j】=dp【i+1】【j】+min(add【i】,del【i】);
最后,如果dp【i】【j-1】是回文串,那么dp【i】【j】=dp【i】【j-1】 + min(add【j】,del【j】);
这一步是关键
for(int k=;k<ch.size();k++)
{
for(int i=,j=k;j<ch.size();i++,j++){
dp[i][j]=inf;
if(ch[i]==ch[j])
dp[i][j]=dp[i+][j-];
else
{
dp[i][j]=min(dp[i][j],dp[i+][j]+money[ch[i]]);
dp[i][j]=min(dp[i][j],dp[i][j-]+money[ch[j]]);
}
} }
| 
 Language: 
Default Cheapest Palindrome 
 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 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. 
#include<iostream>  | 
Cheapest Palindrome(区间DP)的更多相关文章
- POJ 3280 Cheapest Palindrome (区间DP) 经典
		
<题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...
 - POJ 3280 Cheapest Palindrome ( 区间DP && 经典模型 )
		
题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 : 原始模型 ==> 题意和本题差不多,有添和删但是并无代价 ...
 - POJ3280 - Cheapest Palindrome(区间DP)
		
题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...
 - POJ 3280 - Cheapest Palindrome - [区间DP]
		
题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...
 - [poj3280]Cheapest Palindrome_区间dp
		
Cheapest Palindrome poj-3280 题目大意:给出一个字符串,以及每种字符的加入代价和删除代价,求将这个字符串通过删减元素变成回文字符串的最小代价. 注释:每种字符都是小写英文字 ...
 - POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
		
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
 - POJ3280 Cheapest Palindrome 【DP】
		
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6013 Accepted: 29 ...
 - CF 335B - Palindrome 区间DP
		
335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...
 - POJ 3280 Cheapest Palindrome【DP】
		
题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...
 - [luoguP2890] [USACO07OPEN]便宜的回文Cheapest Palindrome(DP)
		
传送门 f[i][j] 表示区间 i 到 j 变为回文串所需最小费用 1.s[i] == s[j] f[i][j] = f[i + 1][j - 1] 2.s[i] != s[j] f[i][j] = ...
 
随机推荐
- bacula 备份恢复
			
一.数据恢复: 在bacula服务器执行: /opt/bacula/etc/ bconsole #进入交互窗口 *restore #输入restore恢复命令 Automatically select ...
 - ArchiMate进行业务架构建模的参考
			
业务服务视图 业务渠道视图 业务服务实现视图 业务角色协作视图 业务流程协作视图 业务流程视图 业务对象视图 产品化业务服务视图 分层视图 除了以上内容,在TOGAF中完整的推荐视图是 在ArchiM ...
 - LeetCode: Keyboard Row
			
代码长了些,但还是比较简单的 public class Solution { public String[] findWords(String[] words) { List<String> ...
 - comet4j开发指南
			
http://blog.csdn.net/majian_1987/article/details/8489738 好多朋友反映,因排版问题,文章部分边缘内容无法查看.尝试过排版,但仍无法显示正常,所以 ...
 - python 运行报错 Process finished with exit code -1073741819 (0xC0000005)
			
发现是由于openpyxl模块导致的,去掉这个模块的内容就能运行,import openpyxl就运行不起来, 将openpyxl卸载了重装, 以及更换了不同的openpyxl版本,都不行,还是运行不 ...
 - 20145231《Java程序设计》第四次实验报告
			
实验四 Android开发基础 实验内容 •安装Android Studio •运行安卓AVD模拟器 •使用Android运行出模拟手机并显示自己的学号 实验步骤 一.安装Android Studio ...
 - Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行
			
原文链接: http://www.cnblogs.com/zouzf/p/3985330.html 在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空 ...
 - Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL
			
原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...
 - python之命令行解析工具argparse
			
以前写python的时候都会自己在文件开头写一个usgae函数,用来加上各种注释,给用这个脚本的人提供帮助文档. 今天才知道原来python已经有一个自带的命令行解析工具argparse,用了一下,效 ...
 - JMeter学习(一)目录介绍
			
JMeter也学了一阵子了,对于基本的操作已了解,再回过头来看看Jmeter的目录,本篇是对于它的目录进行一些简单的介绍. JMeter解压之后打开,根目录如下图: 1.bin:可执行文件目录 2.d ...