POJ 3280 Cheapest Palindrome(DP)
被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费。我觉得挺难的理解的。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[][];
char s1[];
int hash[];
int main()
{
int n,m,i,j,a,b;
char ch[];
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",s1);
memset(dp,,sizeof(dp));
for(i = ;i <= n;i ++)
{
scanf("%s%d%d",ch,&a,&b);
hash[ch[]-'a'] = min(a,b);
}
for(i = m;i >= ;i --)
{
for(j = i;j <= m;j ++)
{
if(s1[i-] == s1[j-])
{
dp[i][j] = dp[i+][j-];
}
else
{
dp[i][j] = min(dp[i+][j]+hash[s1[i-]-'a'],dp[i][j-]+hash[s1[j-]-'a']);
}
}
}
printf("%d\n",dp[][m]);
}
return ;
}
/*
2 5
bbaab
a 1 2
b 10 10
*/
POJ 3280 Cheapest Palindrome(DP)的更多相关文章
- 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 DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- 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。
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)
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...
随机推荐
- linux使用技巧
<1>vim /etc/hosts.deny sshd : 192.168.0.25 :deny //ssh拒绝某ip或网段访问.(原理详见鸟哥基础版18章P56 ...
- JS的trim()方法
去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三 ...
- 一些Linux的路径
系统引导时启动 /etc/rc.d/rc.local
- 【Python】 Django 怎么实现 联合主键?
unique_together¶ Options.unique_together¶ Sets of field names that, taken together, must be unique: ...
- 【Django】Django 如何使用 Django设置的日志?
代码: from django.core.management.base import BaseCommand, CommandError from django.db import models # ...
- codeforces B.Maximum Absurdity 解题报告
题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...
- 【python】filter()
来源:http://www.jb51.net/article/54316.htm filter函数: filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,把序列的每一项 ...
- document.createElement
document.createElement()的用法 document.createElement()是在对象中创建一个对象,要与appendChild() 或 insertBefore()方法联合 ...
- Xshell 中文乱码
Xshell对于嵌入式开发来说,是个非常不错的工具.但或许都有过被中文显示为乱码的问题感觉有点不爽.解决方法其实很简单的,即把xshell编码方式改成UTF-8即可. [文件]–>[打开]–&g ...
- jquery.validate.unobtrusive.js插件作用
在 ASP.NET MVC 中启用 Unobtrusive JavaScript 功能,可以在运行时由服务器端根据Model中设置的验证规则,自动生成客户端验证js代码(结合jquery.valida ...