Cheapest Palindrome(DP)

Cheapest Palindrome(最便宜的回文) 最便宜的回文跟踪所有的奶牛可能是一个棘手的任务,所以农夫约翰已经安装了一个自动化系统。他在每只牛上安装了一个电子ID标签,系统将在扫描仪通过奶牛时读取。每个ID标签的内容当前是从N(1≤N≤26)个不同符号(即小写罗马字母表)的字母表中绘出的长度为M(1≤M≤2000)个字符的单个字符串。奶牛作为他们的调皮生物,有时试图通过向后走向欺骗系统。而ID为“abcba”的奶牛,无论她走哪一个方向,都会读取 阅读详情

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 1: Two space-separated integers: N and M
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
Line 1: A single line with a single integer that is 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
If we insert an “a” on the end to get “abcba”, the cost would be 1000. If we delete the “a” on the beginning to get “bcb”, the cost would be 1100. If we insert “bcb” at the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.

一个字符串,每个字母增添删除都有花费,问最少花费多少,通过增添删除使其成为回文串。

记忆化搜索撒。l和r表示区间
如果s[i] == s[j]那么不用花费,直接到l + 1,r + 1
否则,对于l来说,要么在右面加一个s[i],要么删除s[i],然后转移到l + 1,r
要么同理改变r,然后转移到l,r - 1
在每一个情况中,比较增加和删除同一个字符的最小值,再加上递归的结果,即可,最后两种情况再比较一下。
dp[l][r] = min(dfs(l + 1,r) + min(add,del),dfs(l,r - 1) + min(add,del));

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int n,m,dp[2005][2005];
char s[2005];
map<char,int>ad,de;
int dfs(int l,int r)
{
    if(dp[l][r] != -1)return dp[l][r];
    if(l >= r)return dp[l][r] = 0;
    if(s[l] == s[r])
        return dp[l][r] = dfs(l + 1,r - 1);
    else
        return dp[l][r] = min(dfs(l,r - 1) +  min(ad[s[r]],de[s[r]]),dfs(l + 1,r) + min(ad[s[l]],de[s[l]]));
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    scanf("%d%d",&n,&m);
    scanf("%s",s);
    for(int i = 1;i <= n;i++)
    {
        int add,del;
        char c[10];
        scanf("%s%d%d",&c,&add,&del);
        ad[c[0]] = add;
        de[c[0]] = del;
    }
    memset(dp,-1,sizeof(dp));
    printf("%d\n",dfs(0,m - 1));
    return 0;
}
手把手教你搭建三端口DC-DC变换器(含Boost+Buck电路实战) 本文详细介绍了如何从零开始搭建三端口DC-DC变换器,涵盖Boost和Buck电路的硬件设计、元器件选型、电路板布局、焊接技巧、控制系统设计与调试等关键步骤。通过实战案例和优化建议,帮助工程师避开常见陷阱,提升系统效率和稳定性,适用于光伏、储能等电力电子应用场景。 阅读详情

相关推荐

激光条纹中心提取——Steger

激光条纹中心提取-Steger 好久没有记录了,记录一下今天学习的内容,据说Steger在激光条纹中心提取上神一般的存在。文章和代码都分享给大家了哈,其中有四个文件,3个C++,一个python,python一个抠图文件,3个c++包含背景图+原图, 单张图提取, 多张图提取。 分享代码 #include <opencv2\core.hpp> #include <opencv2\highgui.hpp> #include <opencv2\imgproc.hpp> #i

CharmsLUO的博客 5380

TSP 路径构造算法

TSP 路径构造算法(tour construction algorithm) onezeros@yahoo.cn 前言:Tsp是经典的NP问题之一,其现实意义也是不言而喻的。该问题的研究很有些年头了,现在多集中于新型智能算法,如模拟退火,禁忌搜索,蚁群算法,粒子群算法,遗传算法等。这些算法虽很有效,但使用良好的初始解得时候,效果会更优。本文系统介绍并详细讲解了前辈们的关于构建优良TSP路

小shan在努力~ 3977

【AirSim+PX4】WSL2上运行PX4连接Airsim步骤

在 Linux 命令行中输入以下命令以添加(或改变)环境变量。注意,此处的 IP 地址应为。4.启动 sln 文件的调试以进入 UE4 编辑器,并在 Linux 中启动 PX4。

m0_53538421的博客 1505

vrp 节约算法 c++_VRP路径规划(二)——单车辆TSP

车辆路径规划系列文章:1、VRP路径规划(一)——工具OR-Tools2、VRP路径规划(二)——单车辆TSP3、VRP路径规划(三)——多车辆VRP4、VRP路径规划(四)——有容量限制的CVRP5、VRP路径规划(五)——有时间窗的VRPTW0、前言 TSP问题是VRP问题最早的一个雏形,也就是我们常说的邮递员问题,从邮局出发,选出合适的路线,...

weixin_39830313的博客 2382

高动态范围结构光三维测量技术:从多曝光融合到深度学习的演进与应用

本文深入探讨了高动态范围结构光三维测量技术的演进与应用,从传统的多曝光融合技术到深度学习驱动的创新方案。通过工业案例展示了该技术在提升测量精度和效率方面的显著优势,特别是在处理高反光、复杂表面时的突破性表现。文章还提供了针对不同工业场景的技术选型指南,帮助工程师优化三维测量解决方案。

weixin_29063979的博客 293

链路监控pinpoint3.0.3安装部署(2)

本文详细介绍了Pinpoint 3.0.3分布式链路监控系统的安装部署过程。主要内容包括:1)数据库安装(HBase、MySQL、Redis、Pinot)的详细配置步骤;2)中间件Zookeeper和Kafka的安装;3)Pinpoint三大组件的部署:Collector、Web和Batch的配置文件修改和启动脚本。文章特别强调了使用-starter-exec.jar包的重要性,并提供了各组件所需的详细参数配置,包括数据库连接、Zookeeper地址、Kafka配置等关键信息。

zouzou_888的博客 1372

TSP 路径构造算法(tour construction algorithm)详解

TSP 路径构造算法(tour construction algorithm)onezeros@yahoo.cn 前言:Tsp是经典的NP问题之一,其现实意义也是不言而喻的。该问题的研究很有些年头了,现在多集中于新型智能算法,如模拟退火,禁忌搜索,蚁群算法,粒子群算法,遗传算法等。这些算法虽很有效,但使用良好的初始解得时候,效果会更优。本文系统介绍并详细讲解了前辈们的关于构建优良TSP

onezeros的专栏 1万+

Synopsys逻辑工艺库(DC综合)

逻辑库包括仅与综合过程有关的信息且通过DC用于设计的综合和优化。这一信息包括引脚到引脚的时序、面积、引脚类型和功耗以及其他DC需要的必须数据。逻辑库是一个文本(通常扩展名".lib"),通过使用Library Compiler(LC)编译生成带有扩展名名".db"的二进制文件。以SMIC的逻辑综合库为例:库文件下载地址。

baidu_34971492的博客 2423

Cheapest Palindrome—区间dp

top 题意 给出一个字符串把这个字符串变成回文字符串。 通过最小代价实现字符串的回文化。 会给出每个字母的添加和删除的代价。 分析 字符串S,创建dp数组,f[i] [j] 表示为S[i] 到S[j]之间变成回文字符串的最小代价, 有一点要注意:字符串“abcb”,想变成回文字符串,可以在左边删除a也可以在右边添加a,所以我们会选择代价小的操作,形成回文字符串中,我们指挥使用该字符添加和删除的代...

qq_272941692的博客 1030

量化 insider 爆料! 散户打板必亏?(打板必看!量化资金如何“隔日砸盘”收割散户)

量化开发 insider 爆料💥 散户追的涨停板📉,可能是机构故意做的陷阱!本文从技术视角拆解“涨停陷阱”,帮你看清市场真实逻辑🧠。

XiaoqiangClub的博客 451

【二.图像处理核心技术】【5.形态学操作:腐蚀/膨胀/开闭运算】

想象一下,你拍了一张布满雪花点的老照片,或者扫描了字迹断裂的文档,这时候形态学操作就是你的像素整形师!它们能让模糊的边界变清晰,把断裂的笔画连起来,甚至从X光片里揪出癌细胞。今天我们要聊的,是计算机视觉界的"广场舞双雄"——腐蚀和膨胀。这俩操作就像给像素点们请了个健身教练,要么集体减肥(腐蚀),要么组团增肌(膨胀)。下次当你用美颜相机时,不妨会心一笑——这看似魔法的背后,正是形态学在默默耕耘。腐蚀的规则很简单:如果一个像素点周围的邻居不全白,它就"狗带"(变黑)。:先用砂纸打磨(腐蚀),再喷漆填补(膨胀)

商务合作|问题讨论|交流学习 请联系作者微信,加微信请务必注明来意,博客主页有联系方式 425

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决。 DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的。 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i

靖空间 1365

[PCIE5.0] 2.2.8.9 Optimized Buffer Flush/Fill (OBFF) Message

这段文字讲解的是 优化的缓冲区清空/填充(OBFF)消息,它是 PCI Express(PCIe)规范中的一个可选功能,用于向设备报告平台中央资源的状态。简单来说,OBFF 主要帮助设备了解当前平台资源(如缓存或内存)的状态,从而优化数据的处理和传输。这章重点内容如下。

系统固件工程师 538

POJ3280 Cheapest Palindrome 【DP】

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 2933 Description Keeping track of all the cows can be a tricky task so

长风的专栏 910

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Cheapest Palindrome(dp)

Cheapest Palindrome(dp) 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 conten

Anthcony的博客 250

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[POJ 3280]Cheapest Palindrome[DP]

题目链接:[POJ 3280]Cheapest Palindrome[DP] 题意分析: 有一个长度为m的字符串,全都为小写字母,给出串中每个字母删除和插入的代价,问:要构成回文串,最少需要多少代价? 解题思路: 设dp[i][j]为字符串s[i ~ j]变成回文串需要的最小代价。那么有转移方程: dp[i][j] = dp[i + 1][j - 1] (s[i] == s[j]);

闲停 390

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ 3280 Cheapest Palindrome 【区间DP】

题目链接 题意 给你一串字符串,并给出添加以及删除(在任意位置)每种字符的花费,问把这个字符串变成回文串所需的最少花费 分析 经典的区间DP 状态 设 dp[i][j]⇔将子串S(i,j−1)变成回文串的最小花费 习惯设成前闭后开区间 状态转移方程 如果当前子串最前面和最后面的字符本来就相同,当前的最小花费就等于里面的子串的最小花费。而若不...

a18235146924的博客 92

[dp] poj3280 Cheapest Palindrome

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6598   Accepted: 3197 Description Keeping track of all the cows can be a tricky task so F

童凌的技术博客 550

Cheapest Palindrome(区间DP)

题意:给你一个字符串,将它变成一个回文字符串,添加或删除一个字母都有相应的价值,求最小价值 题解: 区间DP,从小区间推到大区间,dp[i][j]代表将s[i-j]变为回文字符串的最小代价,一个字母肯定是回文字符串,所以dp[i][i] =0, 如果s[i] == s[j],说明这个串已经是一个回文串(区间里面已经是回文字符串),则dp[i][j] = dp[i+1][j-1],当s[i] != ...

weixin_43763889的博客 210

poj 3280 Cheapest Palindrome 区间dp

《挑战》练习题,基础区间dp。

ned_chu的博客 513

ACM dp Cheapest Palindrome

POJ 3280 Cheapest Palindrome 题目大意:(带入样例)给出3种字母,组成长度为4的字符串,增加一个a消耗1000,减少一个a消耗1100,以此类推,问删减各种字母后字符串变成回文最少消耗。 思路:在输入的时候比较增加和减少一个字母的消耗,把较小值存下来。(因为增加一个和减少一个,对于回文串 来说是一样的),然后用dp即可。   #include &lt;...

请赐教 523

[区间dp]Cheapest Palindrome POJ3280

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 singl

Cloth的博客 364

Cheapest Palindrome //区间dp

题目链接:http://poj.org/problem?id=3280 题意:给一个字符串,再给一些字符的删除或添加的代价,让求将字符串变成回文串的最小代价。 思路:对于一个区间如果添加一个字符可以使其回文,那么添加它或者删除与其对称位置的相同字符都可以达到目的,所以可以把每个字符的两个代价合并一下,取最小的那个,然后考虑dp。 定义dp[i][j]dp[i][j]dp[i][j]表示使...

wqq的博客 145

POJ 3280 Cheapest Palindrome 简单DP

观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称。 故两种操作看成一种,只需要保留花费少的那个即可 然后 令 dp[i][j]表示从位置i到j的子串转化为回文串需要的次数 若 s[i]== s[j] 则dp[i][j] = dp[i + 1][j - 1] 否则 d...

weixin_30457881的博客 75

[poj 3280] Cheapest Palindrome dp

Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7884 Accepted: 3822DescriptionKeeping track of all the cows can be a tricky task so Farmer John has installed a

ATOD 768

Cheapest Palindrome 简单dp

E - Cheapest Palindrome Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Description Keeping track of all the cows can be a tricky task so Farmer John has inst

vickey 615
上一篇: Milking Time(DP)
下一篇: Coins(背包DP)
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值