DP E - Cheapest Palindrome
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 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
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
这题真想了很久,感觉无从下手。很容易知道可以利用动态规划解决,但就连添加或删除某字符这个操作都是很难的,更不要谈该如何添加或删除字符串使得成为一个回文字符串。但其实根本不需要实际操作这些。。。我们可以仔细分析一下回文字符串的特性,比如一个字符串"a"和字符串"b"的费用都是0因为它们已经回文,而字符串"ab"的费用是添加/删除'a'或'b'产生的费用。我们并不需要实际操作,甚至不需要知道是删除还是添加,因为可以发现对于一个已经是回文的字符串来说,在其前面或后面加一个字符会出现两种情况:1.还是回文字符串,此时第一个字符和最后一个字符相等。2.变为非回文字符串,这样就需要再在另一边添加同样字符或者删除这个字符以此维持回文,而到底选择哪种只取决于是添加这个字符的费用小还是删除这个字符的费用小。也就是说,我不需要实际操作,只是知道进行了一种操作,然后就能维持回文。所以我们可以由内往外推,从一个字符开始,直到推向整个字符串,推到最后也就是说所给的字符串已经变为回文字符串的最小费用求出。设dp[i][j]表示i~j区间的字符串变为回文字符串所产生的最小费用。由于是从内往外递推的,所以对于上述的情况2来说转移方程为:dp[i][j]
= min(dp[i+1][j],dp[i][j-1]); 情况1的转移方程是有所不同的,因为它此时不需要进行添加/删除操作,而它的最小费用应该由区间(i-1)~(j-1)推过来(比如"aba",区间0~2是由区间1~1推来的,根本不需要添加/删除'a'的费用,假如从0~1推来,0~1已经是含添加/删除‘a'的费用了的,这样肯定最终费用不会最小),转移方程:dp[i][j] = min(dp[i][j],dp[i+1][j-1]); 也可以直接写dp[i][j] = dp[i+1][j-1]; 注意当i ==
j 和i+1 == j的时候i+1>j-1,所以这个要特判一下。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[2200];
int dp[2000][2000];
int cost[2200];
int main()
{
int n,m;
cin>>n>>m;
scanf("%s",s);
char x;
int y=0,z=0;
for(int i=0;i<n;i++)
{
cin>>x>>y>>z;
cost[x-'a']=min(y,z);
}
for(int k=0;k<m;k++)//k代表的是i与j之间的距离
{
for(int i=0;i+k<m;i++)
{
int j=i+k;
dp[i][j]=min(dp[i][j-1]+cost[s[j]-'a'],dp[i+1][j]+cost[s[i]-'a']);
if(s[i]==s[j])
{
if(i==j||i+1==j) dp[i][j]=0;
else dp[i][j]=dp[i+1][j-1];
}
}
}
cout<<dp[0][m-1]<<endl;
return 0;
}
DP E - Cheapest Palindrome的更多相关文章
- POJ 题目3280 Cheapest Palindrome(区间DP)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7148 Accepted: 34 ...
- 【POJ】3280 Cheapest Palindrome(区间dp)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10943 Accepted: 5 ...
- Cheapest Palindrome(区间DP)
个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...
- POJ3280 Cheapest Palindrome 【DP】
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6013 Accepted: 29 ...
- 【POJ - 3280】Cheapest Palindrome(区间dp)
Cheapest Palindrome 直接翻译了 Descriptions 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符 ...
- POJ 3280 Cheapest Palindrome DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
- DP:Cheapest Palindrome(POJ 3280)
价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
随机推荐
- Hibernate学习(一)———— 第一个hibernate工程
一.什么是Hibernate? 轻量级JavaEE应用的持久层框架,是一个完全的ORM框架.(说完这句话,肯定有很多人懵圈了,下面我来一个个解释) 持久化:将我们想要保存的数据保存到硬盘上,也就是我们 ...
- SQL 数据快速查询优化小技巧(仅供参考)
.应尽量避免在where子句中使用!=或<>操作符 .应尽量避免在where子句中使用or来连接条件 如: 可以这样查询 Union all .in 和not in 也要慎用,否则会导致全 ...
- angularjs学习第八天笔记(指令作用域研究)
您好,在前两天对指令的简单了解和系统指令学习后 今天主要研究其指针作用域的相关事情 每一个指令在创建时,其实就构成了自己的一个小的模块单元. 其对于的模块单元都有着其对于的作用域,其中作用域一般有两种 ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第三部分)
Earthstone Keeper Time Limit: 4 Seconds Memory Limit: 65536 KB Earthstone Keeper is a famous ro ...
- 在WindowsPhone开发中使用MVVM设计模式
相信.NET程序员多多少少都听说过MVVM的设计模式,对于一个大一点的项目来说,使用这种设计模式无疑是一种不错的选择, 它提高了程序的可维护性,降低了耦合度,可以实现代码的重用,方便独立开发和进行测试 ...
- 设计模式之适配器模式(Adapter)(6)
简介 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用呢? ...
- IDEA——错误: 找不到或无法加载主类 com.Main
https://blog.csdn.net/gxx_csdn/article/details/79059884 这篇博客非常赞!
- 获取url参数的方法(web)
//获取url参数的方法(web) function GetQueryString(name) { var reg = new RegExp("(^|&)" + n ...
- Android系统启动流程 总结
整体流程大致如下: Android系统的启动,主要是指Android手机关机后,长按电源键后,Android手机开机的过程.从系统角度看,Android的启动程序可分为: 1.bootload ...
- 逻辑回归&线性回归
# coding:utf-8 import numpy as np from sklearn import linear_model, datasets import matplotlib.pyplo ...