Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10943   Accepted: 5232

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

 
 
 
---------------------------------------------------------------------------------------------

吐槽:最近怕是要废了,马上期中考,考完noip。死在作业上了。

分析:

感觉没有什么讲得比他好了  -->>  传送门
 
 
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int inv[],dp[][];
int main()
{
int n,m;
string s;
cin>>n>>m>>s;
for(int i=;i<n;i++)
{
char c;int a,b;
cin>>c>>a>>b;
inv[c]=min(a,b); //这题删减就是套路,既然删1个是回文的话,我们也可以增加1个变成回文,所以只需取最小值
}
for(int i=m-;i>=;i--)
{
for(int j=i+;j<m;j++)
{
dp[i][j]=min(dp[i+][j]+inv[s[i]],dp[i][j-]+inv[s[j]]);
if(s[i]==s[j])
dp[i][j]=min(dp[i][j],dp[i+][j-]);//已经是回文不需要增加费用了
}
}
cout<<dp[][m-];
return ;
}

【POJ】3280 Cheapest Palindrome(区间dp)的更多相关文章

  1. POJ 3280 Cheapest Palindrome (区间DP) 经典

    <题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...

  2. POJ 3280 Cheapest Palindrome ( 区间DP && 经典模型 )

    题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价 ...

  3. POJ 3280 - Cheapest Palindrome - [区间DP]

    题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...

  4. POJ 3280 Cheapest Palindrome(DP 回文变形)

    题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...

  5. (中等) POJ 3280 Cheapest Palindrome,DP。

    Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...

  6. POJ 3280 Cheapest Palindrome【DP】

    题意:对一个字符串进行插入删除等操作使其变成一个回文串,但是对于每个字符的操作消耗是不同的.求最小消耗. 思路: 我们定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价.那么对于 ...

  7. POJ 3280 Cheapest Palindrome(DP)

    题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...

  8. POJ 3280 Cheapest Palindrome 简单DP

    观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...

  9. POJ 3280 Cheapest Palindrome (DP)

     Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...

  10. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

随机推荐

  1. vue.js 源代码学习笔记 ----- $watcher

    /* @flow */ import { queueWatcher } from './scheduler' import Dep, { pushTarget, popTarget } from '. ...

  2. C#项目中怎样创建和使用类和类库

    创建一个类库项目在“文件”菜单上,选择“新建项目”. 在“新建项目”对话框的“模板”窗格中,单击“类库”. 在“名称”框中键入 Persons,再单击“确定”. 把“添加新解决方案”勾选掉,选择“添入 ...

  3. Unity3D 屏幕空间雪场景Shader渲染

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  4. ROS-I工业机器人培训课程资料 2017-06-30

    美国ROS工业联盟于2017年6月6日至8日在德克萨斯州圣安东尼奥市的SwRI举办了ROS工业开发人员培训班.12位与会者代表了一系列不同的组织,包括Bastian Solutions,EWI,Joh ...

  5. MPAndroidChart Wiki(译文)~Part 5

    19. ChartData子类 这篇wiki主要关注ChartData子类的具体介绍.至于此部分没有提及到的ChartData的子类,代表他们没有特性功能需要介绍. BarData 方法 使用 set ...

  6. node.js 之爬虫

    1. cheerio 与 request request:模拟客户端行为,对页面进行请求 cheerio:对服务器端返回的页面进行解析: var cheerio = require('cheerio' ...

  7. Swift 菊花、UIPageControl和UIProgressView

    // Make: 加载 菊花 func _initUIActivityIndicatorView() { let activity = UIActivityIndicatorView(activity ...

  8. MySQL开放外部链接

    问题 指令空格 执行命令netstat -nat | grep :3306的时候,注意grep后一定要有空格!!! 该指令的功能为:让服务器接受远程连接.不过这样只能用于本地连接. 真正外部链接详见 ...

  9. Sublime Text 2 设置文件详解(转)

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  10. BZOJ3924 ZJOI2015 幻想乡战略游戏 【动态点分治】

    BZOJ3924 ZJOI2015 幻想乡战略游戏 Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂 ...