POJ 题目3280 Cheapest Palindrome(区间DP)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7148 | Accepted: 3452 |
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
the minimum cost to change the given name tag.
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
Source

#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?b:a)
int dp[2020][2020],cost[2020];
char str[2020];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",str);
int i;
char c[5];
for(i=1;i<=n;i++)
{
int x,y;
scanf("%s%d%d",c,&x,&y);
cost[c[0]-'a']=min(x,y);
}
int j;
for(j=1;j<m;j++)
for(i=j-1;i>=0;i--)
{
dp[i][j]=min(dp[i+1][j]+cost[str[i]-'a'],dp[i][j-1]+cost[str[j]-'a']);
if(str[i]==str[j])
dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
}
printf("%d\n",dp[0][m-1]);
}
}
POJ 题目3280 Cheapest Palindrome(区间DP)的更多相关文章
- POJ 3280 Cheapest Palindrome (区间DP) 经典
<题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...
- POJ 3280 Cheapest Palindrome ( 区间DP && 经典模型 )
题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 : 原始模型 ==> 题意和本题差不多,有添和删但是并无代价 ...
- POJ 3280 - Cheapest Palindrome - [区间DP]
题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...
- 【POJ】3280 Cheapest Palindrome(区间dp)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10943 Accepted: 5 ...
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- (中等) POJ 3280 Cheapest Palindrome,DP。
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
- POJ 3280 Cheapest Palindrome 简单DP
观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...
- POJ 3280 Cheapest Palindrome【DP】
题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...
随机推荐
- 16个基本颜色关键字 Basic color keywords
16个基本颜色关键字 Basic color keywords Color Color Name HEX RGB black #000000 0,0,0 silver #C0C0C0 192, ...
- web应用动态文档技术
动态生成web文档分为服务器动态生成.客户端动态生成 服务器动态生成文档技术主要有: CGI - 公共网关接口,它是一个允许Web服务器与后端程序以及脚本进行通信的标准化接口.通常是web服务器收到一 ...
- NotePad++常用快捷键。——Arvin
Ctrl+单键 Ctrl+C 复制 Ctrl+X 剪切 Ctrl+V 粘贴 Ctrl+Z 撤消 Ctrl+Y 恢复 Ctrl+A 全选 Ctrl+F 键查找对话框启动 Ctrl+H 查找/替换对话框 ...
- centos7
1.启用Apache(httpd) Centos7默认已经安装httpd服务,只是没有启动.如果你需要全新安装,可以yum install -y httpd 启动服务:systemctl start ...
- text-transform属性
text-transform -- 定义文本的大小写状态,此属性对中文无意义 取值:capitalize | uppercase | lowercase | none | inherit capita ...
- js文件如何最后加载
总结一下,大概有三种方式 1.最简单粗暴的:将js文件放在最后写 2.window.onload当一个文档完全下载到浏览器中时,才会触发window.onload事件.这意味着页面上的全部元素对js而 ...
- Tomcat(免安装版)的安装与配置
一.下载Tomcat Tomcat可以从http://tomcat.apache.org/网站下载,选择任意版本,在 Binary Distributions 下的zip包既是. 二.配置Tomcat ...
- uva 10817(状压dp)
题意:就是有个学校要招老师.要让没门课至少有两个老师可以上.每个样样例先输入三个数字课程数量s,已经在任的老师数量,和应聘的老师数量.已经在任的一定要聘请. 思路是参考了刘汝佳书上的,关键如何状压. ...
- hibernate5ID生成策略
1.uuid2:使用JDK自带的UUID生成36位的ID 2.guid: 3.uuid:生成32位的uuid,不符合ETF RFC 4122标准,已被uuid2取代. 4.uuid.hex:等同uui ...
- java selenium (二) 环境搭建方法一
webdriver 就是selenium 2. webdriver 是一款优秀的,开源的,自动化测试框架. 支持很多语言. 本文描述的是用java Eclipse 如何搭建环境 阅读目录 ...