POJ 3280 Cheapest Palindrome(水题)
题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价为多少。
解法:设d[i][j]表示将字符串中从第i位至第j位变成回文串所需要的代价。若s[i] == s[j],d[i][j] = d[i+1][j-1];否则的话,有四种处理方法。
对xa.......by,可以将其变为xa......b,yxa.....by,a.......by,xa.....byx中的任意一种再处理。所以d[i][j] = min(d[i][j-1] + c2[s[j]], d[i][j-1] + c1[s[j]], d[i+1][j] + c2[s[i]], d[i+1][j] + c1[s[i]])。化简为d[i][j] = min(d[i][j-1] + min(c1[s[j]], c2[s[j]]), d[i+1][j] + min(c1[s[i]], c2[s[i]]))。(此公式看着没有化简,但实际上可以省很多代码,我的代码改了以后少了近200b)
tag:字符串,回文串,dp
/*
* Author: Plumrain
* Created Time: 2013-11-17 21:44
* File Name: DP-POJ-3280.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
const int maxint = ; char s[];
int d[][];
map<char, int > mp; int main()
{
int n, len;
while (scanf ("%d%d", &n, &len) != EOF){
scanf ("%s", s);
char x;
int t1, t2;
mp.clear();
for (int i = ; i < n; ++ i){
x = 'P';
while (!(x >= 'a' && x <= 'z')) scanf ("%c", &x);
scanf ("%d%d", &t1, &t2);
mp[x] = min(t1, t2);
} CLR (d);
for (int i = ; i < len; ++ i)
d[i][i] = ;
for (int i = len-; i >= ; -- i)
for (int j = i+; j < len; ++ j){
if (s[i] == s[j]) d[i][j] = d[i+][j-];
else
d[i][j] = min(maxint, min(d[i+][j] + mp[s[i]], d[i][j-] + mp[s[j]]));
}
printf ("%d\n", d[][len-]);
}
return ;
}
POJ 3280 Cheapest Palindrome(水题)的更多相关文章
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- poj 3280 Cheapest Palindrome
链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...
- 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]
题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
- POJ 3280 Cheapest Palindrome (区间DP) 经典
<题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...
- POJ 3280 Cheapest Palindrome DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
随机推荐
- SQL Server 的远程连接(转载)
SQL Server默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQLServer2012数据库,需要确认以下环节: 1)如果是工作组环境,则需要使用SQL Server身份验证 ...
- 佳博GprinterApp编辑软件使用说明
佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...
- Win10获取管理员/administrator权限的方法
与Win7不同,Win10右键文件夹菜单,是没有“获取管理员权限”这个功能的,但是有时候我们偏偏需要用到这个功能,怎么办呢,可以按照这个办法实现:把下面的这一段代码复制下来放在文本文档中,然后另存为. ...
- 1、大部分社交平台接口不支持https协议。
参考文献来自:http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85 ...
- PHP 数组转JSON数据(convert array to JSON object);
<?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { pu ...
- H5相关
对于容器元素,尤其在做移动端产品时候,我们很自然会让其居中定位: .container { position: absolute; left: %; top: %; transform: transl ...
- odoo view field option, action flage 参数
options JSON object specifying configuration option for the field's widget (including default widget ...
- Python Lib:pyzmq
http://git.oschina.net/gitlab/StartWithCoding/tree/master/example/python/pyzmq
- C# asp:Repeater DataSource List<T>
1. asp:Repeater 数据源为List<T> 2.页面显示 3.行绑定取值
- ubuntu wubi安装注意事项
从这个镜像下载 http://mirrors.163.com/ubuntu-releases/12.10/ ubuntu-12.10-desktop-amd64.iso 和 wubi.exe然后放到同 ...