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. POJ 3630 trie树

    Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...

  2. Vue 函数

    1.转换为大写字符 .toUpperCase() 2.字符串反转  this.message = this.message.split('').reverse().join('') 3.从index开 ...

  3. shiro英语

    Security Manager安全管理人员 Tutorial 辅导的 transient 短暂的 Cipher 密码 Memory 记忆 Access 访问Handy Hint 方便提示separa ...

  4. AI:忧郁的机器人

    1.塔奇克马 塔奇克马研究起来哲学,被缴械....... 2.机器人瓦力 孤独等待EVA的瓦力 3.马文 http://www.guokr.com/post/683881/

  5. js DOM 节点树 设置 style 样式属性

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. python 处理中文 读取数据库输出全是问号

    ref:http://www.cnblogs.com/zhoujie/archive/2013/06/07/problem1.html 1.python连接mssql数据库编码问题 python一直对 ...

  7. Java继承实现接口的抽象类

    1.TestIntace.java package com.chase.abstrac; /** * 接口 * @author Chase * * @date 2013-10-21 下午02:29:1 ...

  8. day009 文件操作

    文件操作 文件路径 d:\test.txt 编码方式 utf-8 gbk... 操作方式 操作方式:只读,只写,追加,读写,写读..... 以什么编码方式储存的文件,就以什么编码打开进行操作. 只读: ...

  9. jsp 多条件组合查询

    web层: public String query(HttpServletRequest request, HttpServletResponse response) throws ServletEx ...

  10. 【转载】Jsp页面传Json数据到服务端,转对象或集合进行数据处理

    需求:1.将页面数据带到服务端并转成对象,2.将页面的集合数据带到服务端转List实现:用ajax请求传递数据,数据格式为json JS方法: testJsonMethod = function(){ ...