Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7869   Accepted: 3816

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,以前求解过这种题,不过是在回文串中添加字符求最少要添加多少字符变成回文串,那种题可以用求正反串的最长公共子序列然后用原串长度
减掉最长公共子序列长度得到。而这题多了个权值,明显就不能用那种方法了(看可以看测试用例)
这里的花费在删除和添加中选个小的就行了,因为删除和添加在本质上对回文串的作用相同。
这里的方法是区间DP:

回文串拥有很明显的子结构特征,即当字符串X是一个回文串时,在X两边各添加一个字符'a'后,aXa仍然是一个回文串,我们用d[i][j]来表示 A[i...j]这个子串变成回文串所需要添加的最少花费,那么对于A[i] == A[j]的情况,很明显有 d[i][j] = d[i+1][j-1] (这里需要明确一点,当i+1 > j-1时也是有意义的,它代表的是空串,空串也是一个回文串,所以这种情况下d[i+1][j-1] = 0)当A[i] != A[j]时,我们将它变成更小的子问题求解,我们有两种决策:
      1、在A[j]后面添加一个字符A[i],花费为d[i+1][j]+value[i];
      2、在A[i]前面添加一个字符A[j],花费为d[i][j-1] + value[j];
      根据两种决策列出状态转移方程为:
            d[i][j] = min{ d[i+1][j]+value[i], d[i][j-1]+value[j] } ;                (每次状态转移,区间长度增加1)
 
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std; int n,m;
char str[];
int value[];
int dp[][]; ///代表从 i,j的最小花费
int main()
{ while(scanf("%d%d",&n,&m)!=EOF){
scanf("%s",str);
while(n--){
char c[];
int v1,v2;
scanf("%s%d%d",c,&v1,&v2);
value[c[]-'a'] = min(v1,v2); ///删掉和添加都可以变成回文串,选小的
}
int len = strlen(str);
memset(dp,,sizeof(dp));
///起点i应当趋0,终点j应当趋len。
for(int i=len-;i>=;i--){
for(int j=i+;j<len;j++){
if(str[i]!=str[j]) {
///更新区间[i ,j]选择[i+1,j]左边添加str[i]在[i,j-1]右边添加str[j]中小的那个
dp[i][j] = min(dp[i+][j]+value[str[i]-'a'],dp[i][j-]+value[str[j]-'a']);
}else{
///相等的话直接添上去
dp[i][j]=dp[i+][j-];
}
}
}
printf("%d\n",dp[][len-]);
}
return ;
}

另外可以枚举区间长度

#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std; int n,m;
char str[];
int value[];
int dp[][]; ///代表从 i,j的最小花费
int main()
{ while(scanf("%d%d",&n,&m)!=EOF){
scanf("%s",str);
while(n--){
char c[];
int v1,v2;
scanf("%s%d%d",c,&v1,&v2);
value[c[]-'a'] = min(v1,v2); ///删掉和添加都可以变成回文串,选小的
}
int len = strlen(str);
for(int l=;l<len;l++){ ///枚举区间长度
for(int i=;i+l<len;i++){
int j = i+l;
if(j>=len) break;
if(str[i]!=str[j]) {
///更新区间[i ,j]选择[i+1,j]左边添加str[i]在[i,j-1]右边添加str[j]中小的那个
dp[i][j] = min(dp[i+][j]+value[str[i]-'a'],dp[i][j-]+value[str[j]-'a']);
}else{
///相等的话直接添上去
dp[i][j]=dp[i+][j-];
}
}
}
printf("%d\n",dp[][len-]);
}
return ;
}

poj 3280(区间DP)的更多相关文章

  1. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  2. POJ 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

  3. POJ 1651 (区间DP)

    题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...

  4. POJ 1141 区间DP

    给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...

  5. POJ 3280 间隔DP

    字符串,每次插入或删除字符需要一定的价格,问:我怎样才能使这个字符串转换成字符串回文,花最少. 间隔DP 当DP到区间[i,j+1]时,我们能够在i-1的位置加入一个str[j+1]字符,或者将在j+ ...

  6. poj 1390 区间dp

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5035   Accepted: 2065 Descriptio ...

  7. POJ 1651 区间DP Multiplication Puzzle

    此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) ...

  8. poj 1141 区间dp+递归打印路径

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30383   Accepted: 871 ...

  9. POJ 3042 区间DP(费用提前计算相关的DP)

    题意: 思路: f[i][j][1]表示从i到j的区间全都吃完了 现在在j点 变质期最小是多少 f[i][j][0]表示从i到j的区间全都吃完了 现在在i点 变质期最小是多少 f[i][j][0]=m ...

随机推荐

  1. 谈谈Javascript的匿名函数

    JQuery 里面有这么一种代码: (function(){ // code here })(); 当一个匿名函数被括起来,然后再在后面加一个括号,这个匿名函数就能立即运行起来,神奇吧! 要说匿名函数 ...

  2. taotao服务测试http请求需要返回json时出现406错误处理

    @Test public void doPost() throws Exception { CloseableHttpClient httpClient = HttpClients.createDef ...

  3. 理解LINUX LOAD AVERAGE的误区

    一直不解,为什么io占用较高时,系统负载也会变高,偶遇此文,终解吾惑. uptime和top等命令都可以看到load average指标,从左至右三个数字分别表示1分钟.5分钟.15分钟的load a ...

  4. ReentrantLock和synchronized区别和联系?

    相同:ReentrantLock提供了synchronized类似的功能和内存语义,都是可重入锁. 不同: 1.ReentrantLock功能性方面更全面,比如时间锁等候,可中断锁等候,锁投票等,因此 ...

  5. stout代码分析之五:UUID类

    UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识. stout中UUID类继承自boost::uuids::uuid.api如下: random, 产生一个UUID对象 ...

  6. [解决] User [dr.who] is not authorized to view the logs for application

    在hadoop集群启用权限控制后,发现job运行日志的ui访问不了, User [dr.who] is not authorized to view the logs for application ...

  7. [LeetCode] string整体做hash key,窗口思想复杂度O(n)。附来自LeetCode的4例题(标题有字数限制,写不下所有例题题目 T.T)

    引言 在字符串类型的题目中,常常在解题的时候涉及到大量的字符串的两两比较,比如要统计某一个字符串出现的次数.如果每次比较都通过挨个字符比较的方式,那么毫无疑问是非常占用时间的,因此在一些情况下,我们可 ...

  8. [Luogu 3178] HAOI2013 树上操作

    [Luogu 3178] HAOI2013 树上操作 一道比模板还简单的难以置信的裸HLD省选题. 大约是需要long long. #include <cstdio> #include & ...

  9. [Luogu 1168] 中位数

    中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊.(笑) 功能十分简单的Treap即能满足需求--只需要插入与查找第大的功能. 插入第i个数时,如果i是奇数,随即询问当前排名第(i+ ...

  10. Gradle编译时下载依赖失败解决方法

    如果Gradle在编译的时候没有在本地仓库中发现依赖,就会从远程仓库中下载,默认的远程仓库为mavenCentral(),也就是http://repo1.maven.org/maven2/,但是往往访 ...