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 ...
随机推荐
- 【微收藏】FourShadows.js – 时间感知的算法驱动的图标阴影JS库
废话一箩筐,筐筐有心得 不小心养成了一个刷微博的习惯,主要还是关注一些前端人士,学习一些前端方面的知识,看到大家都有一些刷微博的小习惯.有的是转发一下,转发内容来一个标记(MARK).也有评论中标记为 ...
- 实现一个简单的vue-router
所有项目的源代码都放在我的github上,欢迎大家start: https://github.com/Jasonwang911/my-vue-router 首先来看下vue-router的使用: im ...
- LinQ是什么?
•LINQ(发音:Link)是语言级集成查询(Language INtegrated Query) •LINQ是一种用来进行数据访问的编程模型,使得.NET语言可以直接支持数据查询 •LINQ的目标是 ...
- Oracle入门《Oracle介绍》第一章1-2 Oracle 实例
Oracle实例:是后台进程和内存结构的集合 一.内存结构 1.Oracle 实例启动时分配系统全局区 a.数据库信息存储于SGA,由多个数据库进程共享 1.共享池是对SQL.PL/SQL程序进行语法 ...
- [PHP] 数据结构-输出链表倒数第k个结点PHP实现
输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后移动,当第一个结点到达末尾的时候,第二个结点所在位置就是倒数第k个节点了 <?php clas ...
- canvas学习和滤镜实现
最近学习了 HTML5 中的重头戏--canvas.利用 canvas,前端人员可以很轻松地.进行图像处理.其 API 繁多,这次主要学习常用的 API,并且完成以下两个代码: 实现去色滤镜 实现负色 ...
- blfs(systemv版本)学习笔记-编译安装openssh软件包
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! openssh项目地址:http://www.linuxfromscratch.org/blfs/view/8.3/postlf ...
- sql server: quering roles, schemas, users,logins
--https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/managing- ...
- Vue和React的对比
今晚我们来搞一搞Vue和React的对比好吧,话不多说今天我们直接开搞可好,各位小老板,开始吧 1. react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入, 所以在react中,是 ...
- React中props
今天让我们开启新的篇章好吧,来搞一搞React,以下所有操作都是我个人的一些理解,如果有错吴还请指出,想要看更全的可以去React官网可能一下子好吧 昨天按摩没到位,导致今天身体不太行,撸码千万别苦了 ...