POJ3280 Cheapest Palindrome 【DP】
| 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 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
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】的更多相关文章
- POJ 3280 Cheapest Palindrome【DP】
题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...
- POJ3280 - Cheapest Palindrome(区间DP)
题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...
- POJ1159:Palindrome【dp】
题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]= ...
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- 如何使用SVN协调代源代码,多人同步开发
转自linFen原文如何使用SVN协调代源代码,多人同步开发 1.什么是SVN SVN是一种版本管理系统,前身是CVS,是开源软件的基石.即使在沟通充分的情况下,多人维护同一份源代码的一定也会出现混乱 ...
- [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js
You often use the same data in different ways across pages. This lesson walks you through setting up ...
- 【计算机】基本概念的理解 —— 沙盒(sandbox)、交互式计算/编程/应用
web scraper:网络铲: scraper:n. 刮刀:铲土机:守财奴: 1. 交互式计算/编程/应用(interactive computing/application/programming ...
- Kinect 骨骼映射---Let me dance for U!
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45583443 作者:ca ...
- JavaEE 技术选型建议,server配置,部署策略
基础设施环境 # 总体採用 centos6.5 + nginx + tomcat7.0 负载均衡:nginx 配置,使用 nginx 作为负载均衡.权重配置. 在web层做到水平扩展. 以及配置日志格 ...
- Android官方数据绑定框架DataBinding(一)
还记得在博客<高逼格UI-ASD(Android Support Design)>的開始曾经说过,Android最新推出了一个官方的数据绑定框架-Data Binding Library. ...
- BootStrap让两个控件在一行显示
<div class="row"> <div> <label class="form-inline">参加单位:<in ...
- BAT实习内推笔试卷(第一场)——个人答案以及分析
第一题: 给定一个长度不小于2的数组arr. 写一个函数调整arr,使arr中要么全部的偶数位上都是偶数,要么全部的奇数位上都是奇数上. 要求:假设数组长度为N.时间复杂度请达到O(N),额外空间复杂 ...
- [Javascript] Javascript 'in' opreator
If you want to check whether a key is inside an Object or Array, you can use 'in': Object: const obj ...
- Android的NDK开发(1)————Android JNI简介与调用流程
1.JNI简介 JNI全称为Java Native Interface(Java本地调用).从Java1.1开始,JNI成为java平台的一部分,它允许Java代码和其他语言写的代码(如C&C ...