POJ 3280 Cheapest Palindrome (DP)

POJ 3280 Cheapest Palindrome (DP)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。



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

题意:一串字母序列。经过添加或删减某个字符使得序列成为回文,添加和删减都有花费,问花费最少多少。

设dp[i][j]为从i到j的花费。
dp[i][j] = min ( dp[i+1][j]+cost[i] , dp[i][j-1]+cost[j] );  ( a[i] != a[j] )
dp[i][j] = dp[i+1][j-1] ( a[i] == a[j] )
cost[]里存的就是每一个字符删减或者添加的较小的值,由于删掉a[i]和在j后面添加一个a[i]效果是一样的,仅仅需比較两者的花费谁更小

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
int n,m,cost[30],dp[2007][2005];
char s[2005],cc[3];
int main()
{
    scanf("%d%d%s",&n,&m,s);
    for(int i=0;i<n;i++) {
        int xx,yy;
        scanf("%s %d %d",cc,&xx,&yy);
        cost[ cc[0]-'a' ] = min(xx,yy);
    }
    for(int j=1;j<m;j++)
        for(int i=j-1;i>=0;i--)
             if( s[i] == s[j] ) dp[i][j] = dp[i+1][j-1];
             else dp[i][j] = min( dp[i+1][j]+cost[ s[i]-'a' ] ,dp[i][j-1]+cost[ s[j]-'a' ] );
    printf("%d\n",dp[0][m-1]);
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117433.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 函数极限的定义

    函数极限的定义严格定义设函数y=f(x)y=f(x)y=f(x)在点x0x_0x0​的某个去心邻域内有定义,即存在ρ>0\rho>0ρ>0,使O(x0,ρ)\{x0}⊂Df\mathbf{O}(x_0,\rho)\backslash\{x_0\}\subsetD_fO(x0​,ρ)\{x0​}⊂Df​如果存在实数AAA,对于任意给定的ε>0\varepsilon>0ε>0,可以找到δ>0\delta>0δ>0,使得当0<∣x−x0∣

    2022年5月4日
    44
  • 如何从从官网下载各个版本的jquery「建议收藏」

    如何从从官网下载各个版本的jquery「建议收藏」许多前端的小伙伴们可能跟我一样有选择强迫症,对于一些工具、软件等都偏爱从官网下载,尽管许多非官方网站上已经有现成的,但还是从心理上感觉官网的更正规。如今的jquery版本已经是相当多了。在jquery官网的首页上只提供了为数不多的较为流行的版本供我们下载。但是出于各种情况的考虑,我们想要自己需要的某一版本该如何从官网获取呢?步骤也是相当简单,jquery官网虽然为了页面的简洁性并未在Downl…

    2022年5月25日
    37
  • 数据挖掘算法和实际应用案例

    数据挖掘算法和实际应用案例第一次写博客,这次主要引用Little_Rookie大佬的一篇博客,主要来说明一下数据挖掘的主要算法和分类,以及实际生活中的应用案例,同时也是为了方便自己以后的学习。如何分辨出垃圾邮件”、“如何判断一笔交易是否属于欺诈”、“如何判断红酒的品质和档次”、“扫描王是如何做到文字识别的”、“如何判断佚名的著作是否出自某位名家之手”、“如何判断一个细胞是否属于肿瘤细胞”等等,这些问题似乎都很专业,都…

    2022年6月16日
    32
  • jquery事件delegate()方法用法详解[通俗易懂]

    jquery事件delegate()方法用法详解[通俗易懂]我们先看官方是怎么说delegate()方法,delegate()方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数,使用delegate()方

    2022年7月2日
    24
  • stm32 带通滤波器_带通滤波器详解_带通滤波器工作原理_带通滤波器原理图

    stm32 带通滤波器_带通滤波器详解_带通滤波器工作原理_带通滤波器原理图带通滤波器(band-passfilter)是一个国家允许使用特定频段的波通过发展同时进行屏蔽其他频段的设备。比如RLC振荡回路问题就是这样一个可以模拟带通滤波器。带通滤波器是一种滤波器,它可以在一定的频率范围内通过频率分量,但将其他范围内的频率分量衰减到非常低的水平,与带阻滤波器的概念形成对比。模拟带通滤波器的一个例子是电阻电感电容电路(RLC电路)。这些滤波器也可以通过将低通滤波器与高通…

    2022年5月2日
    32
  • 2020/3/28微信小程序笔记

    2020/3/28微信小程序笔记1.左右布局display:flex;flex-direction:row;2.修改switch组件大小style=’zoom:.6’具体数值看个人而定3.仿个性签名关键词:textareatextarea限制字数关键词:maxlength\minlength4.设置渐变色css background: -webkit-linear-gradient(lef…

    2022年8月18日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号