poj3280 Cheapest Palindrome
来源:互联网 发布:数据采集卡的作用 编辑:程序博客网 时间:2024/06/05 18:12
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
大意:给你一个字符串,只包含小写英文字母,然后给你每种小写字母增加和删除这个字符的花费。
让你求这个字符串最终能构成回文串的最小花费。
没想到这是个区间dp,果然字符串什么的基本上要去往最大公共子串这样上面去想。
dp的题,关键就是找好dp数组的维数以及状态转化方程。
思路:dp[i][j]表示区间i~j构成回文串的最小花费。
类比lcs然后对于每个区间有3种情况:
1.s[i]==s[j]那么dp[i][j]=dp[i+1][j-1].
2.不相等.
一种是dp[i][j]=dp[i+1][j]+min(add(s[i]),del(s[i]));
另一种是dp[i][j]=dp[i][j-1]+min(add(s[j]),del(s[j]));
然后自然就是这两种里面取一个小的值。
对于每个字符,每次操作就是两种删除或者添加,我们自然只会选择花费小的,所以上面的+min(add(s[i]),del(s[i]))可以直接换掉,然后实际上对于每个字符就是一种操作。
#include <iostream>#include <cstdio>using namespace std;const int MAXN=2000+10;const int inf=1e9;char s[MAXN];int dp[MAXN][MAXN];int cost[30];int n,m;int main(){ int i,j,k; char name[2]; scanf("%d%d",&n,&m); scanf("%s",s); int add,del; for(i=0;i<n;++i) { scanf("%s",name); name[0]-='a'; scanf("%d%d",&add,&del); cost[name[0]]=min(add,del); } for(k=1;k<m;++k) for(i=0,j=k;j<m;++i,++j) { if(s[i]==s[j])dp[i][j]=dp[i+1][j-1]; else { dp[i][j]=min(dp[i+1][j]+cost[s[i]-'a'],dp[i][j-1]+cost[s[j]-'a']); } } printf("%d\n",dp[0][m-1]); return 0;}
- POJ3280 Cheapest Palindrome
- POJ3280 Cheapest Palindrome
- poj3280 Cheapest Palindrome
- POJ3280 Cheapest Palindrome 【DP】
- POJ3280 Cheapest Palindrome
- [dp] poj3280 Cheapest Palindrome
- 【POJ3280】【Cheapest Palindrome】
- POJ3280--Cheapest Palindrome
- poj3280 Cheapest Palindrome
- poj3280 Cheapest Palindrome
- POJ3280-Cheapest Palindrome
- poj3280 Cheapest Palindrome
- POJ3280 Cheapest Palindrome
- POJ3280-Cheapest Palindrome
- POJ3280:Cheapest Palindrome(区间DP)
- POJ3280 Cheapest Palindrome 区间DP
- 区间dp poj3280 Cheapest Palindrome
- poj3280 Cheapest Palindrome(区间dp)
- 图论(6)--邻接表的建立与入度出度
- Android中跨进程通讯的4种方式
- KAFKA源码阅读——FetchRequestPurgatory, ProducerRequestPurgatory
- ThinkPHP 学习笔记
- 链式前向星
- poj3280 Cheapest Palindrome
- LinuxMint下安装eclipse
- 自主shell脚本编程
- jQuery练习5——val()练习
- 洛谷 P1278 单词游戏
- RAC、响应式编程的学习
- poj3450 Corporate Identity
- java删除某目录下所有文件
- 数据结构之图的应用