poj3280 Cheapest Palindrome
来源:互联网 发布:surefire 淘宝山寨 编辑:程序博客网 时间:2024/06/06 03:17
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
给你一个m个字符的字符串以及每种字符增加或减少一个的价格,问最少多少价格能使其变为回文序列。
思路:因为增加和减少都是相对的,所以可以得出删减每种字符的价格,然后用dp[i][j]表示i到j区间使这些字符变为回文串的最小价格。如果s[i]==s[j],dp[i][j]=min(dp[i][j],dp[i+1][j-1]);如果s[i]!=s[j],dp[i][j]=min(dp[i+1][j]+w[s[i]-'a'],dp[i][j-1]+w[s[j]-'a'].
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;#define maxn 2060#define inf 999999999int dp[2005][2006];char s[maxn],str[10];int w[30];int main(){ int n,m,i,j,c,d,len; while(scanf("%d%d",&n,&m)!=EOF) { scanf("%s",s+1); for(i=1;i<=n;i++){ scanf("%s%d%d",str,&c,&d); w[str[0]-'a']=min(c,d); } if(m==1){ printf("0\n");continue; } for(i=1;i<=m;i++){ dp[i][i]=0; } for(i=1;i<=m-1;i++){ if(s[i]==s[i+1])dp[i][i+1]=0; else dp[i][i+1]=min(w[s[i]-'a' ],w[s[i+1]-'a']); /*printf("--->%d\n",dp[i][i+1]);*/ } if(m==2){ printf("%d\n",dp[1][2]);continue; } for(len=3;len<=m;len++){ for(i=1;i+len-1<=m;i++){ j=i+len-1; dp[i][j]=inf; if(s[i]==s[j]){ dp[i][j]=dp[i+1][j-1]; } else{ dp[i][j]=min(dp[i+1][j]+w[s[i]-'a' ],dp[i][j-1]+w[s[j]-'a' ]); } } } printf("%d\n",dp[1][m]); } 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)
- MFC 键盘响应
- js校验金额支持小数点后两位
- 宣告主权 我为自己代言!!!!!!!!
- CodeForces 46D Parking Lot(线段树区间合并)
- 自定义toast、toast重复显示、切换界面toast自动消失
- poj3280 Cheapest Palindrome
- 15/9/7/SharePreference/内部存储/外部存储
- hadoop集群默认配置和常用配置
- altera fpga 约束
- Linux中如何实现Signal?
- Snooker(2060)
- 关于类和对象
- js倒计时
- Redis中连接池使用的相关问题