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. ADO.NET Data Service

    关于ADO.NET Entity Framework部分的内容见ADO.NET Entity Framework(1-4) http://www.cnblogs.com/foundation/arch ...

  2. 苹果开发之COCOA编程(第三版)下半部分

    第十八章:Image和鼠标事件 1.NSResponderNSView继承自NSResponder类.所有的事件处理方法都定义在NSResponder类中.NSResponder申明了如下方法:- ( ...

  3. php 算法之------------怎样打印出下图

    自己偶尔看到了下图.于是用php打印出下图. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGluZ2ppZ29uZ3Np/font/5a6L5L2T/f ...

  4. split_brain

    脑裂 系统中两个或多个部分开始独立工作

  5. PECL的安装和使用

    下载并安装pear脚本 cd /usr/local/php/bin/ curl -o go-pear.php http://pear.php.net/go-pear.phar ./php go-pea ...

  6. Android Development Note-01

    Eclipse快捷键: 导包:ctrl+alt+o 格式化代码:ctrl+alt+f   MVC: M——Model V——View C——Control   android程序界面如何设计.调试 U ...

  7. Java for LeetCode 088 Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...

  8. pymysql 模块的使用

    一 . pymysql 的下载和使用 在python 中操作数据库需要用到 pymysql 模块.    (1) . pymysql 模块的下载 pip3 install pymysql (2) . ...

  9. CLion提示can't find stdio.h等错误

    先上解决办法,启动参数如下: $ LANG=en_US.UTF-8 /path/to/clion.sh 查了好知久,竟然就由于编码的原因.可是Ubuntu已经设置为英文UTF-8,还是可以通过上面的方 ...

  10. 《CSS权威指南(第三版)》---第三章 结构和层叠

    这章主要讲的是当某个对象被选择器多次提取使用样式之后的一些冲突性解决方案: 1.特殊性:指的是当多个效果作用的时候的最终选择: 这个规则用0,0,0,0来比较.其中:内联式是1,0,0,0  ID选择 ...