poj 3280(区间DP)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7869 | Accepted: 3816 |
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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std; int n,m;
char str[];
int value[];
int dp[][]; ///代表从 i,j的最小花费
int main()
{ while(scanf("%d%d",&n,&m)!=EOF){
scanf("%s",str);
while(n--){
char c[];
int v1,v2;
scanf("%s%d%d",c,&v1,&v2);
value[c[]-'a'] = min(v1,v2); ///删掉和添加都可以变成回文串,选小的
}
int len = strlen(str);
memset(dp,,sizeof(dp));
///起点i应当趋0,终点j应当趋len。
for(int i=len-;i>=;i--){
for(int j=i+;j<len;j++){
if(str[i]!=str[j]) {
///更新区间[i ,j]选择[i+1,j]左边添加str[i]在[i,j-1]右边添加str[j]中小的那个
dp[i][j] = min(dp[i+][j]+value[str[i]-'a'],dp[i][j-]+value[str[j]-'a']);
}else{
///相等的话直接添上去
dp[i][j]=dp[i+][j-];
}
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
另外可以枚举区间长度
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <string.h>
#include <math.h>
using namespace std; int n,m;
char str[];
int value[];
int dp[][]; ///代表从 i,j的最小花费
int main()
{ while(scanf("%d%d",&n,&m)!=EOF){
scanf("%s",str);
while(n--){
char c[];
int v1,v2;
scanf("%s%d%d",c,&v1,&v2);
value[c[]-'a'] = min(v1,v2); ///删掉和添加都可以变成回文串,选小的
}
int len = strlen(str);
for(int l=;l<len;l++){ ///枚举区间长度
for(int i=;i+l<len;i++){
int j = i+l;
if(j>=len) break;
if(str[i]!=str[j]) {
///更新区间[i ,j]选择[i+1,j]左边添加str[i]在[i,j-1]右边添加str[j]中小的那个
dp[i][j] = min(dp[i+][j]+value[str[i]-'a'],dp[i][j-]+value[str[j]-'a']);
}else{
///相等的话直接添上去
dp[i][j]=dp[i+][j-];
}
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
poj 3280(区间DP)的更多相关文章
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
- POJ 1651 (区间DP)
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...
- POJ 1141 区间DP
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...
- POJ 3280 间隔DP
字符串,每次插入或删除字符需要一定的价格,问:我怎样才能使这个字符串转换成字符串回文,花最少. 间隔DP 当DP到区间[i,j+1]时,我们能够在i-1的位置加入一个str[j+1]字符,或者将在j+ ...
- poj 1390 区间dp
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5035 Accepted: 2065 Descriptio ...
- POJ 1651 区间DP Multiplication Puzzle
此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) ...
- poj 1141 区间dp+递归打印路径
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30383 Accepted: 871 ...
- POJ 3042 区间DP(费用提前计算相关的DP)
题意: 思路: f[i][j][1]表示从i到j的区间全都吃完了 现在在j点 变质期最小是多少 f[i][j][0]表示从i到j的区间全都吃完了 现在在i点 变质期最小是多少 f[i][j][0]=m ...
随机推荐
- 解决jsp两种提交方式乱码 的方法
解决中文乱码 ---post提交方式 需要在处理页面添加request.setCharacterEncoding("utf-8"); 制定请求的编码,调用一下request.ge ...
- SRM13 T3 花六游鸟小(结论题)
哇这题是真的喵,HR智商太高辣 这题的难点就是看了题解之后怎么证明题解里的结论... 结论①:深度大于logm的点肯定能达到最大值 证明:显然一个西瓜的属性里0数量一半1数量一半我们取到的1数量最少, ...
- 剑桥offer(41~50)
41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...
- 用CSS3实现的addidas阿迪达斯标志LOGO
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>用CSS3实现的ad ...
- 搭建openresty需要注意到的地方
openresty的完整包放在百度云盘linux目录下 一键安装openresty ./install.sh 安装好后,修改nginx.conf配置文件 cd /usr/local/openresty ...
- apache的作用和tomcat的区别
经常在用apache和tomcat等这些服务器,可是总感觉还是不清楚他们之间有什么关系,在用tomcat的时候总出现apache,总感到迷惑,到底谁是主谁是次,因此特意在网上查询了一些这方面的资料,总 ...
- mysql 常用总结
centos7 安装mysql 数据库安装参考:http://www.cnblogs.com/longrui/p/6071581.htmlhttps://www.cnblogs.com/yoursou ...
- zigbee ------ JN5169低功耗设置
低功耗睡眠设置Power Manager (PWRM) PWRM_vInit() 如果进入睡眠模式,设置芯片进入何种睡眠模式 PWRM_eScheduleActivity()设置进入睡眠多长时间(时钟 ...
- springboot-部署到centos7
环境 系统:centos7 64位 安装jdk 第一步:下载 先进入官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...
- sun.security.x509.CertAndKeyGen;找不到
导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...