poj 3280 Cheapest Palindrome(不简单)

ROS从入门到精通2-1:机器人3D可视化工具——Rviz 本文详细介绍了Rviz是什么?为什么要使用这个工具?清晰地列出了Rviz的安装流程和界面的基本功能元素;讲解了Rviz基本数据类型与话题,并用两个实例深入理解Rviz数据可视化的过程 阅读详情

Cheapest Palindrome

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4539 Accepted: 2209

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.

Source

USACO 2007 Open Gold

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
 * 题目大意:给定一个字符串,可以删除增加字符,每个操作都有其相应的代价,求出将字符串转换成回文串的最小代价
 * 不简单的dp,递归加记忆化搜索,状态方程不太好想。
 */
#define MIN(x, y) ((x) < (y) ? (x) : (y));
char data[2005];	//接收输入的数据
int dp[2005][2005];
int cost[27];
/*
 * 求转换成回文的最小代价
 * @param data:要处理的字符串
 * @param start:字符串的开始下标
 * @param end:字符串的结尾下标
 */
int find(char *data, int start, int end)
{
	if(dp[start][end] != -1)
	{
		return dp[start][end];
	}
	if(start >= end)
	{
		return 0;
	}
	/* 
	 * 最小值=min(消去开始字符的代价, 消去结尾字符的代价, 
	 * 同时消去开始结尾字符的代价(若开始、结束字符相等))
	 */
	dp[start][end] = MIN(find(data, start + 1, end) + cost[data[start] - 'a'], 
			find(data, start, end - 1) + cost[data[end] - 'a']);
	if(data[start] == data[end])
	{
		dp[start][end] = MIN(dp[start][end], find(data, start + 1, end - 1));
	}
	return dp[start][end];
}
int main()
{
//	freopen("in.txt", "r", stdin);
	int n, m;
	while(scanf("%d%d%*c", &n, &m) == 2)
	{
		scanf("%s%*c", data);
		int a, b;
		char temp;
		int i;
		for(i = 0; i < n; ++ i)
		{
			scanf("%c %d %d%*c", &temp, &a, &b);
			cost[(int)(temp - 'a')] = MIN(a, b);
		}
		memset(dp, -1, sizeof(dp));
		int res = find(data, 0, m - 1);
		printf("%d\n", res);
	}
	return 0;
}


Outlook Express 6 (OE6) for Win7/Win8/Win10 英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。 立即下载

相关推荐

Veeam Backup 13 通过UI界面高效备份VMware ESXi虚拟机的完整指南

本文提供了一份使用Veeam Backup 13图形化界面高效备份VMware ESXi虚拟机的完整指南。内容涵盖从环境准备、添加vCenter/ESXi主机、配置备份存储库,到创建备份任务、设置保留策略与计划,以及执行备份和验证成功的关键步骤。通过直观的UI操作,管理员可以轻松实现虚拟机数据的可靠保护与快速恢复。

weixin_29322553的博客 454

POJ 3280 Cheapest Palindrome【DP之经典回文问题】

原题链接:http://poj.org/problem?id=3280 CSUST:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=20437#problem/C 转载来自:http://blog.sina.com.cn/s/articlelist_1836701754_0_1.html Cheapest Palindrom

free斩 1649

Python表白代码:“ 星光月夜烟花皆归你,我也归你”(满天烟花盛开、附番外玫瑰)

导语 "慢品人间烟火色 闲观人间岁月长" ———致自己​​​ ​ ???? 遇见我以后,我们的故事就开始了,愿你历经山河,仍觉得人间值得????。 ​ 星光月夜烟花皆归你,我也归你。关于烟花????大家都​知道多少?有多少表白故事情节都发生在烟 花下,想必木木子????不用说大家也知道叭~​​ 今天这则小短文就是关于烟花的故事!你准备好跟我一起进入烟花的世界了嘛? ​正文 “每一句文案,都有一个故事,你仔细听” ​ 1)环境安装???? 准备好:.

顾木子吖的博客 5万+

POJ 3280 Cheapest Palindrome(区间DP)

Cheapest Palindrome 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 tha

nudt_oys的博客 548

VS2022命令行编译C++报错?一招解决‘fatal error C1034: iostream: 不包括路径集‘

本文针对VS2022命令行编译C++时常见的'fatal error C1034: iostream: 不包括路径集'错误,深入剖析了其根源在于环境变量缺失。文章提供了永久配置用户环境变量(PATH、INCLUDE、LIB)的一劳永逸解决方案,确保在任何终端中都能直接使用cl命令进行编译,并给出了进阶配置与常见错误排查指南。

weixin_29011667的博客 310

【零基础教程】HEC-RAS 1D/2D全流程模拟指南|从水动力到水质建模一次搞定!

HEC-RAS由美国陆军工程兵团水文研究中心研制,具有完整的一二维耦合的水动力及水环境模拟功能,也具有闸、涵洞、桥梁以及泵站等水利构筑物的模拟能力;同时也可以与HEC研制的水文模型(HMS)、洪水淹没风险和损失分析软件(FDA)等耦合,是免费软件中使用最方便、应用最广泛的软件之一。2.HEC-RAS与HEC-HMS的耦合(不详细讲HEC-HMS)3.淹没风险分析软件HEC-FDA与HEC-RAS耦合。案例十八:HEC-RAS与HEC-HMS的耦合。案例十九:HEC-RAS与HEC-FDA的耦合。

weixin_48230888的博客 877

(POJ 3280)Cheapest Palindrome <DP> 回文数变形

Cheapest Palindrome DescriptionKeeping 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 syste

STILLxjy 631

WD西部数据2TB,2.5寸移动硬盘,因为磁头坏了,长时间通电导致划片划伤,维修过程通过反复更换磁头

WD西部数据2TB,2.5寸移动硬盘,因为磁头坏了,长时间通电导致划片划伤,维修过程通过反复更换磁头,才能利用专业设备,里面找到,镜像,把内容数据恢复出来。 1.说一下经历,借用的别人移动硬盘,去帮别人维修,拷数据出来,别人的文件,数据比较多,且复杂,而且是在PE系统,接口什么的是2.0,没有很好的思考就全选复制拷贝了; 2,拷贝的过程,极其慢,文件多,且文件有损坏,移动硬盘一直处于通电的状态,大概从下午,两点多开始100多G的数据,一直卡住,到,晚上7 。8点拷完还是没拷完。打开的时候就已经,就一直

weixin_43766920的博客 4313

POJ-3280 Cheapest Palindrome DP 鶸的DP解题报告

题目:Cheapest PalindromeTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 12011 Accepted: 5693DescriptionKeeping track of all the cows can be a tricky task so Farmer John has installed a system ...

Reast1nPeace的博客 566

【华为Hilink SDK Linux系统开发】目录 开源一小步

目录 【华为Hilink开发】第一章:华为hilink平台简介及开发流程 【华为Hilink开发】第二章:华为hilink SDK网络适配、网关添加删除 【华为Hilink开发】第三章:华为hilink SDK子设备添加、删除 【华为Hilink开发】第四章:华为hilink SDK子设备控制及同步 【华为Hilink开发】第五章:华为hilink SDK适配遇到的问题 ...

开源一小步 3628

poj(3280)Cheapest Palindrome(区间dp)

题目链接:http://poj.org/problem?id=3280 题意:给出一个由m中字母组成的长度为n的串,给出m种字母添加和删除花费的代价,求让给出的串变成回文串的代价。 分析:我们知道求添加最少的字母让其回文是经典dp问题,转化成LCS求解。这个是一个很明显的区间dp 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价。 那么对于dp【

大神养成中..... 5562

【WRF-FDDA】观测 Obs nudging 的配置与使用说明(中)

WRF-FDDA

WW、forever的博客 286

[poj]3280 Cheapest Palindrome 题解

[poj]3280 Cheapest Palindrome 区间dp 题意: 给你长度为m的字符串,其中有n种字符,每种字符都有两个值,分别是插入这个字符的代价,删除这个字符的代价,让你求将原先给出的那串字符变成一个回文串的最小代价。 M<=2000 设 dp[i][j] 为区间 i~j 的回文串的最小代价 现在考虑怎样从别的状态转移到 区间i~j 三种情况 首先 ...

547478006的博客 195

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ3280 Cheapest Palindrome

题目链接:http://poj.org/problem?id=3280 题目大意就是给你一个字符串,和字符串中每一个字母删除或者添加所需要付出的代价,问把它变成一个回文字串所需要的最少的代价。 首先明确一个问题,如果我们在字符串中某一个位置删除一个字符,那么一定能找到一个等价的添加的方法,所以可以把删除和添加统一到一起,然后这道题的状态就简单了。 dp[i][j]表示的是区间[i, j]内形

浅雨歌 1019

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

POJ-3280-Cheapest Palindrome

POJ-3280-Cheapest Palindrome 传送门 这道题。区间dp 求把所给串变成回文串的最小代价。 题目给出由n个小写字母构成的长度为m的串。 然后把每个字母增加和删除的代价给出来了 问最小代价 其实这个地方的增加和删除其实差不多把。。 开头删等同于在末尾加上去 比如abcb 你在开头删除a和在末尾加上a的效果是一样的=-=对吧 然后我们取删除和添加的最小值就行了 存在w[]里面...

qq_44624316的博客 202

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ - 3280Cheapest Palindrome-经典区间DP

POJ - 3280 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Keeping track of all the cows ca

qq_18661257的专栏 1249

[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

POJ3280 Cheapest Palindrome(区间dp)

我们可以在字符串的任何位置添加或删除字符,产生一个比原始字符串更长或更短的字符串。行:每行包含三个空格分隔的实体:一个输入字母表的字符和两个整数,分别是增加和删除该字符的费用。标签,以便无论奶牛从哪个方向走过,它们的读数都一样。的奶牛无论正着走还是倒着走,读数都是一样的;第1行: 一行有一个整数,是改变给定名称标签的最小成本。标签是电子的,每个字符的插入或删除都有一个成本。标签的内容和插入或删除每个字母的成本,找出改变。标签的最小成本,使其满足约翰的要求。,成本依据添加或删除的字符的不同而不同。

zaiyang遇见 562

POJ - 3280 Cheapest Palindrome 区间DP

传送门:POJ3280 题意:给定一个字符串,要将其变成回文串,删除一个字母和添加一个字母都有不同的花费,问最小花费是多少。 思路:这题我真是没有思路,太弱了。。 正解:dp[i][j]表示将[i,j]区间的字符串变成回文串的最小花费,然后又如下转移: 如果s[i] == s[j]  那么 dp[i][i] = dp[i + 1][j - 1] 否则 dp[i][j] = min(dp[

WA是一笔财富 306

【动态规划】POJ 3280 Cheapest Palindrome

这段时间要沉迷刷题一段时间了,就让CSDN陪我一起吧! 一、题目大意 这道题的题意很简单,第一行给出两个整数N和M,N表示组成字符串的字符种类数,M表示字符串的字符个数,然后第二行给出一个字符串,之后N行,每行给出一个字符添加和删除的开销,求解要把整个字符串通过添加或删除字符调整为回文字符串所需的最小开销。 二、题目思路以及AC代码 这题可能在大佬面前就是送分题了,但对我还是需要思考的。 首先...

Johnson的博客 182

Poj 3280 Cheapest Palindrome

题目大意:长度为M的字符串,可以增加或删除字符,使其构成回文,增加或删除不同的字符有不同的花费,求最小花费。 思路:其实dp很难逃出3种思路: 1、一维线性dp:每次考虑i时,选择最优子问题要么在i-1,要么在1...i-1里; 2、二维线性dp:考虑(i,j)子问题时,选择最优子问题要么在(i+1,j)、(i,j-1),要么在i 3、树形dp:考虑i节点最优时,选择子节点最优,一般融合了

Detective_Xin的专栏 1099

POJ-3280 Cheapest Palindrome

题目链接: http://poj.org/problem?id=3280 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 ...

Blank_Tt的博客 313

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

长风的专栏 911

POJ 3280 Cheapest Palindrome

Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K 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 ea...

忘梦心的博客 378

POJ3280 Cheapest Palindrome(区间DP) ——菜鸟的ACM之旅

POJ3280 Cheapest Palindrome 题目大意 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符Add与Delete的代价,求将S变为回文串的最小代价和。(字符全部是小写字母) 思路 区间DP,dp[i][j]代表i,j区间内字符串成为回文的最小花费,那么我们需要从小范围到大范围扩展,我们到dp[i-1][j+1]的时候,有两种可能,第一种s[i-1] != s[j+1] ,我们需要修改两边的字符,并将小于i j+1

u012570745的博客 216
上一篇: poj 1159 Palindrome
下一篇: poj 1050To the Max dp+暴力
hefa
博客等级 码龄16年 1粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值