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 变成回文的最小代价.那么对于 ...
随机推荐
- python-05
首先是安装工具 Linux 安装mysql:mysql-server 安装python-mysql模块: python-mysqldb Windows 下载安装mysql python操作mysql模 ...
- node config
@echo off title node_5560 cd /d %~dp0 java -jar selenium-server-standalone-.jar ^ -role node ^ -Dweb ...
- 在cwcity空间上安装phpmyadmin
上传程序后,安装phpMyAdmin-4.1.4-all-languages.3715384168.zip 出现了错误: Warning: realpath() [function.realpath] ...
- Cognos10安装注意事项
cognos10用db2做content management注意事项 1. 建议用UTF-8格式字符2. 建议pagesize用8K或者8K以上3. 新建数据库缓冲池pagesize和以上1.2设置 ...
- nodejs新建服务器
var http = require('http');// var optfile = require('./models/optfile'); http.createServer(function ...
- 使用Axis2建立WebService
Axis是apache重量级的WebService框架,虽然相比Xfire和CXF而言相对比较臃肿,但是企业中最常用的就是Axis,Axis2是Axis的升级版: 建立一个最简单的Axis2 W ...
- vs中部分快捷键
ctrl + r ctrl + r ctrl 按两次r修改变量名(修改被引用到的变量名),不同于ctrl + f(修改全部相同名字的) ctrl +r ctrl + m 先选中一部分da ...
- 关于css
已经学了四天的css.现在对于css的了解还很肤浅,首先,我对基础的还不是很了解. 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Shee ...
- db2命令
把远程的数据库信息加载到本地 第一步,catalog server端的node ,命令如下: db2 catalog tcpip node db2node remote hostname server ...
- CentOS_7.2安装MySQL_5.7
一.安装依赖包和开发工具: yum install vim vim-enhanced wget zip unzip telnet ntsysv compat* apr* nasm* gcc gcc* ...