POJ3280(DP)
题目大意是说一个字符串,每插入或者删除一个字符都需要一定的代价,问怎样可以使这个字符串变成一个回文串,且花费最小。
首先明确的就是如果已经将区间[i,j]整理成为一个回文串(不管中间有多少个字符或者是以什么字符开头或者结尾),当DP到区间[i,j+1]时,我们可以在i-1的位置添加一个str[j+1]字符,或者将在j+1处的字符删除,得到一个新的回文串,而且我们这两步操作都没有借助或者影响区间[i,j]的情况。
因此,那我们就可以将添加或者删除整合在一起,对字符str[j+1]的操作就按照添加和删除中花费最小的一个计算。写出状态转移方程:
DP[j][i] = MIN(DP[j+1][i]+cost[str[j]-'a'], DP[j][i-1]+cost[str[i]-'a']);
if(str[i] == str[j])DP[j][i] = MIN(DP[j][i],DP[j+1][i-1]);
这里的j是按照i-1到0枚举的,由于可能str[i] == str[j],因此也可以不操作,再将 不操作 与 添加或删除区间头或尾 比较。
当然,其实最初的想法是扩展出来四个状态,就是首删除,首添加,尾删除,尾添加,由此扩展开来求一个最小值
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABTUAAABHCAIAAABZOTSvAAAgAElEQVR4nO1dy3IcyXXtz4EwUvBXYAuS7AWX/ANDoQc2Dq3wAQNJIUuwIxQKrya4EMzwwsZIDseEd8MILgbdDfABdDVADknwIQ7QQFd6Ua/MmzezsrKrMru6T8VZkN1Vhdsn82bek4+bgwd7j4FOIYSIbgPgDSEESlBmI7oNALDOgA968wbqvKmLbkNPEYC6z0czIV8f3n3e5mtnX3+xstQBwBKidOVBdFNWHmhleg1EdYSN6DYAwDoDPujNG6jzpi66DT1Fx9RdPhf89fyrRV8OfQ4AUQB9HpTr6DYA3kBUR9iIbgMArDPgg968gTpv6qLb0FN0SZ1RnAshhPj0aLH3Q58DQBRAnwflOroNgDcQ1RE2otsAAOsM+KA3b6DOm7roNvQU3VEnLWuXpXgl2q9GTw33G9T7V5+E9Cyrz5WXXFz2lDoAWGZAnwflOroNgDcQ1RE2otsAAOsM+KA3b6DOm7roNvQUnVH39OsPBqX9xbsr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kfe6iPb0OoguF1ufPARvXjneyo7zl2mZvMG+oG28GSgTatdgTgAofWCOMZ6/S5J2YvhfT99lA/m026j9KZqPktjzcVcVslMzG09unL+/O34rp+2ydXjqe3h6fXw8nN+PpfJQYFumpsOR3AJYT8EFv3kCdN3XRbegpQN2i1C1x7wkAXQD6PCjXTney+boTsb/YNBS/3vtI7C/ZiNHSAlEdYSO6Df2DNcJ48ToPL87fiqcvRRY9ZAHEKJlZIozsnuzxItXNHfT5ygM+6M0bqPOmLroNPQWoW5S6Je49AaALQJ8H5drlNvYsMUsqckcwh6JZM6gBBIjqCBvRbegfFo0w5hKyCON2lNyOktl4Onv+bZ4X5+yNOL2cj5Kb4eR6lNwOE3MSWgl6DkhgyQEf9OYN1HlTF92GngLULUrdEveeANAFoM+Dcu14p77/vJV9EXQTF8LxJkBUR9iIbkP/4B9h3I6S6tyX0SSV5wGGk9tRMstS3STvxPlb8ezV/ORiNpx8N5xc10cYpkQPwHIDPujNG6jzpi66DT0FqFuUuuXsPQGgM0CfB+U6ug2ANxDVETai29A/+EcYanihBhnDyWw4uTm9nJ+/FZMrMbkSL16np5e3o+T6m7Pvjid38X840AHgg968gTpv6qLb0FOAukWpQ+8JrBkqfS5w4cKFCxcuXLhw4cKFCxcuXLEvzJ+HGAuJbgPgjeyKbsaSAFT4YNEZAPoIv0LvSjz79u7k8maYfBpOPg0TzACsJuCD3ryBOm/qotvQU4C6RalD7wmsGcrr/wEwCfzfdLPRGgAAAABJRU5ErkJggg==" alt="" width="715" height="38" />
#include <stdio.h>
#include <string.h>
#define mem(a) memset(a,0,sizeof(a))
#define MIN(a,b) ((a) < (b) ? (a) : (b)) int DP[][],cost[],N,M;
char str[]; int main()
{
while(~scanf("%d%d", &M, &N))
{
mem(DP); mem(str); mem(cost);
scanf("%s%*c",str);
char ch; int x, y;
for(int i=;i<M;i++)
{
scanf("%c %d %d%*c", &ch, &x, &y);
cost[ch-'a'] = MIN(x,y);
}
for(int i=;i<N;i++)
{
for(int j=i-;j>=;j--)
{
DP[j][i] = MIN(DP[j+][i]+cost[str[j]-'a'], DP[j][i-]+cost[str[i]-'a']);
if(str[i] == str[j])DP[j][i] = MIN(DP[j][i],DP[j+][i-]);
}
}
printf("%d\n", DP[][N-]);
}
return ;
}
POJ3280(DP)的更多相关文章
- dp基础大概 (8.6)
一些前言: 据说动态规划会用排序,数据结构来进行乱搞优化操作 动态规划滴核心是个啥呢?状态表示和状态转移 设状态:哪些因素会影响到最终答案,就把哪些因素用数组的维度表示出来 要充分描述,也要简洁 举个 ...
- [poj3280]Cheapest Palindrome_区间dp
Cheapest Palindrome poj-3280 题目大意:给出一个字符串,以及每种字符的加入代价和删除代价,求将这个字符串通过删减元素变成回文字符串的最小代价. 注释:每种字符都是小写英文字 ...
- poj3280 Cheapest Palindrome(回文串区间dp)
https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...
- poj3280 区间dp
//Accepted 15880 KB 250 ms #include <cstdio> #include <cstring> #include <iostream> ...
- POJ3280 - Cheapest Palindrome(区间DP)
题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...
- poj3280(区间dp)
题目连接:http://poj.org/problem?id=3280 题意:给定一个长度为m(m<=2000)的小写字母字符串,在给定组成该字符串的n(n<=26)个字符的添加和删除费用 ...
- Cheapest Palindrome [POJ3280] [区间DP] [经典]
一句话题意:每个字母添加和删除都相应代价(可以任意位置 增加/删除),求把原串变成回文串的最小代价 Description 保持对所有奶牛的跟踪是一项棘手的任务,因此农场主约翰已经安装了一个系统来实现 ...
- POJ3280 Cheapest Palindrome 【DP】
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6013 Accepted: 29 ...
- POJ3280 Cheapest Palindrome (区间DP)
dp[i][j]表示将字符串子区间[i,j]转化为回文字符串的最小成本. 1 #include<cstdio> 2 #include<algorithm> 3 #include ...
随机推荐
- [转]JavaScript 的性能优化:加载和执行
原文链接:http://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/index.html?ca=drs- JavaScript 的性能优化: ...
- 想找个计算器当本命?来试试UWP应用《纸书科学计算器》
久违了.上次在博客园发文还是4年前,正是高中参加NOIP的时候.这4年里发生了很多事,乃至再次看到过去的文章时,仿佛看到了自己也不熟悉的风景.最近很想把我的博客重新拾起来,慢慢灌溉,写一些微不足道的技 ...
- acdream 1682 吃不完的糖果(环形最大子段和)
Problem Description 娜娜好不容易才在你的帮助下"跳"过了这个湖,果然车到山前必有路,大战之后必有回复,大难不死,必有后福!现在在娜娜面前的就是好多好多的糖果还有 ...
- 用于分类的决策树(Decision Tree)-ID3 C4.5
决策树(Decision Tree)是一种基本的分类与回归方法(ID3.C4.5和基于 Gini 的 CART 可用于分类,CART还可用于回归).决策树在分类过程中,表示的是基于特征对实例进行划分, ...
- 【转】15 个用于 GitHub 的 Chrome 插件
原文网址:http://webres.wang/15-useful-chrome-extensions-for-github/ 对于 GitHub 你了解多少呢?其实,它是一个基于 Git 托管的 W ...
- 纯css实现鼠标感应弹出二级菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 脚本乐园 Shell中命令行选项和参数的处理
在Linux的Shell中怎样处理tail -n 10 access.log这样的命令行选项呢?这是被别人问起的一个问题,好好学习了一下,进行总结如下:在bash中,可以用以下三种方式来处理命令行参数 ...
- 【HTTP】GET和POST的区别
[HTTP]GET和POST的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有四种:GET.POST.PUT.DELETE. URL全程是资源描述符,用于描述网上的资源.以上四种方式就是 ...
- vector容器总结.xml
1 清空所有元素 m_itemVector.clear(); 2 遍历 vector<ITEM_CHECK>::iterator iter=m_itemVector.b ...
- 浅谈jquery选择器
首先来说说jquery选择器的优势: 1.简洁的写法 2.支持css1.0到3.0选择器 3.完善的处理机制. 再来说说分类: jquery选择器分为基本选择器.层次选择器.属性选择器.基本过滤选择 ...