POJ - 3280

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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 Status

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
abcb
a 1000 1100
b 350 700
c 200 800

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,就能够了。
/*
Author: 2486
Memory: 16120 KB Time: 16 MS
Language: G++ Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2000+5;
int dp[maxn][maxn];
char str[maxn],op[10];
int cost[30];
int n,m,a,b;
int main() {
while(~scanf("%d%d",&n,&m)) {
scanf("%s",str+1);
for(int i=0; i<n; i++) {
scanf("%s%d%d",op,&a,&b);
cost[op[0]-'a']=min(a,b);
}
memset(dp,0,sizeof(dp));//当中dp[i][j]代表着[i,j]这个区间
for(int j=2; j<=m; j++) {
for(int i=j-1; i>0; i--) {
if(str[i]==str[j])dp[i][j]=dp[i+1][j-1];
else dp[i][j]=min(dp[i][j-1]+cost[str[j]-'a'],dp[i+1][j]+cost[str[i]-'a']);
}
}
printf("%d\n",dp[1][m]);
}
return 0;
}

POJ - 3280Cheapest Palindrome-经典区间DP的更多相关文章

  1. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  2. POJ 1160 经典区间dp/四边形优化

    链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...

  3. Cheapest Palindrome(区间DP)

    个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...

  4. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  5. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  7. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  8. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

  9. POJ 2176 Folding(区间DP)

    题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...

随机推荐

  1. HTML基础练习

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. windows phone传感器

    Windows phone中的传感器主要包括加速计传感器.罗盘传感器.陀螺仪传感器等 加速计传感器 Accelerometer类是加速传感器的接口,Accelerometer类位于Windows.De ...

  3. 通过getSystemServices获取手机管理大全

    getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.以下介绍系统相应的服务. 传入 ...

  4. QS之force(3)

    Example in project - First force signals in certain time and then noforce signals after some time. # ...

  5. Yoga710笔记本Win10和Ubuntu系统共存

    联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...

  6. OpenCV: kalman滤波的代码段

    序言:在我的疲劳检测工程 AviTest中!显示框为320*240,使用OpenCV的kalman滤波算法,可以实现简单的锁相追踪-实现对眼球的位置锁定. 代码如下: CvPoint Wishchin ...

  7. (转)C#开发微信门户及应用(6)--微信门户菜单的管理操作

    http://www.cnblogs.com/wuhuacong/p/3701961.html 前面几篇继续了我自己对于C#开发微信门户及应用的技术探索和相关的经验总结,继续探索微信API并分享相关的 ...

  8. CDR服装设计-旗袍款式图

    在服装行业中的服装款式设计.图案设计和面料设计等方面,CorelDRAW是一款常用绘图设计软件,用CorelDRAW绘制款式图比手绘更容易表达服装结构.比例.图案.色彩等要素,服装款图主要目的是为了更 ...

  9. 【转】在VMware中为Linux系统安装VM-Tools的详解教程

    在VMware中为Linux系统安装VM-Tools的详解教程 如果大家打算在VMware虚拟机中安装Linux的话,那么在完成Linux的安装后,如果没有安装Vm-Tools的话,一部分功能将得不到 ...

  10. eas之打开窗体

    UIContext uiContext=new UIContext(this);IUIWindow uiWindow=UIFactory.createUIFactory(UIFactoryName.E ...