Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10301   Accepted: 4931

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.

题意:

有一个长度为m的字符串,其中有n个不同的字母,每个字母有自己的添加和删除的花费,求如何增删字母,可以使字符串变为回文串且花费最小。

对于一个字母来说,增和删的效果是一样的,所以可将它的花费看做增删的最小值。

设dp[i][[j]为i~j区间内字母变为回文的最小花费,则它只由dp[i+1][j]+cost[i]和dp[i][j-1]+cost[j]决定。

当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1]。则dp[i][j]为两者之间的最小值。

AC代码:

 //#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int MAXN=; int dp[MAXN][MAXN];
int cost[]; int cmp(int a,int b){
if(a>b)
a=b;
return a;
} int main(){
ios::sync_with_stdio(false);
int n,m;
string s;
char c;
while(cin>>n>>m&&n&&m){
cin>>s;
int x,y;
for(int i=;i<n;i++){
cin>>c>>x>>y;
cost[c-'a']=min(x,y);
}
memset(dp,,sizeof(dp));
for(int i=m-;i>=;i--){
for(int j=i+;j<m;j++){
dp[i][j]=cmp(dp[i+][j]+cost[s[i]-'a'],dp[i][j-]+cost[s[j]-'a']);
if(s[i]==s[j]){
dp[i][j]=cmp(dp[i+][j-],dp[i][j]);
}
}
}
cout<<dp[][m-]<<endl;
}
return ;
}

POJ-3280的更多相关文章

  1. poj 3280【区间dp】

    poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739  uva 10453 ...

  2. poj 3280 Cheapest Palindrome

    链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...

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

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

  4. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

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

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

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

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

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

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

  8. DP:Cheapest Palindrome(POJ 3280)

    价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...

  9. POJ 3280 Cheapest Palindrome(DP)

    题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...

  10. POJ 3280 Cheapest Palindrome(DP)

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

随机推荐

  1. Jquery获取iframe中的元素

    iframe与父页面之间相互获取元素的方法: 1.从父页面中获取iframe页面中的元素: 用法: $(window.frames["iframe_include_adverse" ...

  2. 【iOS开发-51】案例学习:动画新写法、删除子视图、视图顺序、延迟方法、button多功能使用方法及icon图标和启动页设置

    案例效果: (1)导入所需的素材,然后用storyboard把上半截位置和大小相对固定的东西布局起来.当然,这些控件也要定义成对应地IBOutlet和IBAction方便兴许使用它们. 注意:本案例在 ...

  3. PostgreSQL 封装操作数据库方法

    /// <summary> /// 模块名:操作postgres数据库公共类 /// 作用:根据业务需求对数据库进行操作. /// 注:系统中的公共方法,根据需要,逐一引入 /// 作者: ...

  4. 九度OJ 1164:旋转矩阵 (矩阵运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3188 解决:1245 题目描述: 任意输入两个9阶以下矩阵,要求判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0.90.180. ...

  5. Java反射详解(转)

    原文地址:http://www.importnew.com/17616.html 动态语言 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如众所 ...

  6. Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree

    http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/ #include <iostream ...

  7. WPF区时浏览小程序

    在深圳已经工作了一个月了,之前做WinForm的,现在做WPF,每天加班到晚上10点,比之前累.学习新技术也是有个过程的,我就从基础的语法和 界面布局做起.这是我仿着做一个小软件. 效果图赏析 在原基 ...

  8. C++函数模板的显示调用与隐式调用

    C++函数模板可以显示调用与可以隐式调用 首先定义函数模板: template <class T> inline const T& c_max (const T& a, c ...

  9. BZOJ 3990 [SDOI2015]排序

    题解: 首先很容易看出各个操作是互不影响的,即对于一个合法的操作序列,我们可以任意交换两个操作的位置而不影响合法性. 因此我们可以忽略操作先后的影响,只考虑这个操作是否会出现在操作序列中. 如果用2n ...

  10. 51Nod 1627 瞬间移动 —— 组合数学

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 1627 瞬间移动  基准时间限制:1 秒 空间限制:1 ...