Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6013   Accepted: 2933

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.

Source

dp[i][j]表示从字符串的位置i到位置j转换成回文串所须要的最小费用,当str[i]==str[j]时,dp[i][j]=dp[i+1][j-1];否则dp[i][j]=min(dp[i+1][j]+cost[i], dp[i][j-1]+cost[j]);

#include <stdio.h>
#include <string.h> #define maxn 2010 char str[maxn];
int dp[maxn][maxn];
struct Node {
int add, del;
} cost[26]; int min(int a, int b) {
return a < b ? a : b;
} int getDele(char ch) {
return cost[ch - 'a'].del;
} int getAdd(char ch) {
return cost[ch - 'a'].add;
} int main() {
// freopen("stdin.txt", "r", stdin);
int N, M, i, j, id, step, len;
char buf[2];
scanf("%d%d", &N, &M);
scanf("%s", str);
memset(cost, -1, sizeof(cost));
while(M--) {
scanf("%s", buf);
id = buf[0] - 'a';
scanf("%d%d", &cost[id].add, &cost[id].del);
}
len = strlen(str);
for(step = 1; step < len; ++step)
for(i = 0; i + step < len; ++i) {
if(str[i] == str[i+step])
dp[i][i+step] = dp[i+1][i+step-1];
else {
dp[i][i+step] = min(min(dp[i+1][i+step] + getDele(str[i]), dp[i][i+step-1] + getDele(str[i+step])), min(dp[i+1][i+step] + getAdd(str[i]), dp[i][i+step-1] + getAdd(str[i+step])));
}
}
printf("%d\n", dp[0][len-1]);
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

POJ3280 Cheapest Palindrome 【DP】的更多相关文章

  1. POJ 3280 Cheapest Palindrome【DP】

    题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...

  2. POJ3280 - Cheapest Palindrome(区间DP)

    题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...

  3. POJ1159:Palindrome【dp】

    题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]= ...

  4. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  5. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  8. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  9. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

随机推荐

  1. windows apache 跳转 tomcat 代理

    需求是这样的 服务器有tomcat和apache两个服务器 t端口号是8080,a端口是80 比如javaweb的域名是 www.XXX.com:8080 phpweb的域名是  a.XXX.com ...

  2. mootools常用特性和示例(基础篇1)

    网上关于mootools这个库的信息很少. 公司一些老的项目用到了mootools库,因为要维护,所以接触到了mootools. mootools(文档)官网:http://www.chinamoot ...

  3. DateTime与timeStamp的转换

    DateTime转换为timeStamp: DateTime dt = DateTime.Now;            DateTime startTime = TimeZone.CurrentTi ...

  4. jQuery获取多种input值的方法(转)

    获取input的checked值是否为true: 第一种: if($("input[name=item][value='val']").attr('checked')==true) ...

  5. Linux下搭建Memcached缓存系统

    首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...

  6. 使用C#版本的gdal库打开hdf文件

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近应同事的请求帮忙研究下使用C#版的gdal库读取hdf文件,今天算是有一点成果,特地做一些记录. 首先是编译C#版 ...

  7. php求二叉树的深度(1、二叉树就可以递归,因为结构和子结构太相似)(2、谋而后动,算法想清楚,很好过的)

    php求二叉树的深度(1.二叉树就可以递归,因为结构和子结构太相似)(2.谋而后动,算法想清楚,很好过的) 一.总结 1.二叉树就可以递归,因为结构和子结构太相似 2.谋而后动,算法想清楚,很好过的 ...

  8. 嵌入式平台下的ldd

    x86平台有ldd可以很方便的查看对库的依赖关系,但在嵌入式linux环境中没有这个命令,替而代之是 CC=$(CROSS_COMPILE)gcc LDD=$(CROSS_COMPILE)readel ...

  9. PDF编译出现错误解决的方法————————【Badboy】

    额 今天  在编译PDF时发现使用了一下STL中的z数值极限居然编译只是. return GetRangeConstraint(value <= std::numeric_limits::max ...

  10. php实现求一个数的质数因子

    php实现求一个数的质数因子 一.总结 一句话总结:这么简单的题目,还是把变量定义的位置和自增的位置写错. 1 <?php 2 $num=trim(fgets(STDIN)); 3 //如果$n ...