[dp] poj3280 Cheapest Palindrome
来源:互联网 发布:淘宝如何设置搭配套餐 编辑:程序博客网 时间:2024/05/28 15:43
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 4abcba 1000 1100b 350 700c 200 800
Sample Output
900
Hint
题目大意是说一个字符串,每插入或者删除一个字符都需要一定的代价,问怎样可以使这个字符串变成一个回文串,且花费最小。
首先明确的就是如果已经将区间[i,j]整理成为一个回文串(不管中间有多少个字符或者是以什么字符开头或者结尾),当DP到区间[i,j+1]时,我们可以在i-1的位置添加一个str[j+1]字符,或者将在j+1处的字符删除,得到一个新的回文串,而且我们这两步操作都没有借助或者影响区间[i,j]的情况。
因此,那我们就可以将添加或者删除整合在一起,对字符str[j+1]的操作就按照添加和删除中花费最小的一个计算。写出状态转移方程:
DP[j][i] = MIN(DP[j+1][i]+cost[str[j]-'a'], DP[j][i-1]+cost[str[i]-'a']);
if(str[i] == str[j])DP[j][i] = MIN(DP[j][i],DP[j+1][i-1]);
这里的j是按照i-1到0枚举的,由于可能str[i] == str[j],因此也可以不操作,再将 不操作 与 添加或删除区间头或尾 比较。
当然,其实最初的想法是扩展出来四个状态,就是首删除,首添加,尾删除,尾添加,由此扩展开来求一个最小值。
代码:
#include <cstdio>#include <cstring>#define mem(a) memset(a,0,sizeof(a))#define MIN(a,b) ((a) < (b) ? (a) : (b))int DP[2005][2005],cost[30],N,M;char str[2005];int main(){ while(~scanf("%d%d", &M, &N)) { mem(DP); mem(str); mem(cost); scanf("%s%*c",str); char ch; int x, y; for(int i=0;i<M;i++) { scanf("%c %d %d%*c", &ch, &x, &y); cost[ch-'a'] = MIN(x,y); } for(int i=1;i<N;i++) { for(int j=i-1;j>=0;j--) { DP[j][i] = MIN(DP[j+1][i]+cost[str[j]-'a'], DP[j][i-1]+cost[str[i]-'a']); if(str[i] == str[j])DP[j][i] = MIN(DP[j][i],DP[j+1][i-1]); } } printf("%d\n", DP[0][N-1]); } return 0;}
- POJ3280 Cheapest Palindrome 【DP】
- [dp] poj3280 Cheapest Palindrome
- POJ3280:Cheapest Palindrome(区间DP)
- POJ3280 Cheapest Palindrome 区间DP
- 区间dp poj3280 Cheapest Palindrome
- poj3280 Cheapest Palindrome(区间dp)
- [区间dp]poj3280 Cheapest Palindrome
- POJ3280-Cheapest Palindrome-区间dp
- POJ3280*Cheapest Palindrome题解动态规划DP
- poj3280--Cheapest Palindrome(区间dp)
- POJ3280 Cheapest Palindrome (区间dp)
- 【区间DP】POJ3280[Cheapest Palindrome]题解
- POJ3280 Cheapest Palindrome
- POJ3280 Cheapest Palindrome
- poj3280 Cheapest Palindrome
- POJ3280 Cheapest Palindrome
- 【POJ3280】【Cheapest Palindrome】
- POJ3280--Cheapest Palindrome
- HDU 1728 逃离迷宫 单方向BFS 或者DFS加剪枝
- 蓄水池随机抽样
- “印象笔记”的分享
- 从零开始打造一个新闻订阅APP之Android篇(四、实现仿微信发图界面)
- 对泛型栈的学习
- [dp] poj3280 Cheapest Palindrome
- 会员卡管理系统技术解析(二)Java Web SSH 框架的搭建
- 我的Cocos2d-x学习笔记(五)CCLayer、CCLayerColor、CCLayerGradient、CCLayerMultiplex
- IIC读写AT24C02代码1——通过串口命令控制R/W
- x86汇编基础记录,以便查询
- 爱立信
- Mybatis深入之初始化过程
- unity-sysfont: Unity3D plugin for rendering dynamic text using device/platform fonts
- openwrt启动脚本分析