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.
 
 
分析:(别人的)
这题真想了很久,感觉无从下手。很容易知道可以利用动态规划解决,但就连添加或删除某字符这个操作都是很难的,更不要谈该如何添加或删除字符串使得成为一个回文字符串。但其实根本不需要实际操作这些。。。我们可以仔细分析一下回文字符串的特性,比如一个字符串"a"和字符串"b"的费用都是0因为它们已经回文,而字符串"ab"的费用是添加/删除'a'或'b'产生的费用。我们并不需要实际操作,甚至不需要知道是删除还是添加,因为可以发现对于一个已经是回文的字符串来说,在其前面或后面加一个字符会出现两种情况:1.还是回文字符串,此时第一个字符和最后一个字符相等。2.变为非回文字符串,这样就需要再在另一边添加同样字符或者删除这个字符以此维持回文,而到底选择哪种只取决于是添加这个字符的费用小还是删除这个字符的费用小。也就是说,我不需要实际操作,只是知道进行了一种操作,然后就能维持回文。所以我们可以由内往外推,从一个字符开始,直到推向整个字符串,推到最后也就是说所给的字符串已经变为回文字符串的最小费用求出。设dp[i][j]表示i~j区间的字符串变为回文字符串所产生的最小费用。由于是从内往外递推的,所以对于上述的情况2来说转移方程为:dp[i][j]
 = min(dp[i+1][j],dp[i][j-1]); 情况1的转移方程是有所不同的,因为它此时不需要进行添加/删除操作,而它的最小费用应该由区间(i-1)~(j-1)推过来(比如"aba",区间0~2是由区间1~1推来的,根本不需要添加/删除'a'的费用,假如从0~1推来,0~1已经是含添加/删除‘a'的费用了的,这样肯定最终费用不会最小),转移方程:dp[i][j] = min(dp[i][j],dp[i+1][j-1]); 也可以直接写dp[i][j] = dp[i+1][j-1]; 注意当i ==
 j 和i+1 == j的时候i+1>j-1,所以这个要特判一下。
 
分析:
这题看了别人的题解,一开始并没有看懂,后来把样例带进去算就差不多弄懂了,和上面的差不多。拿样例来说吧,就是已知m(这个字符串的长度),求这个字符串变成回文序列的最小花费的钱。就可以划分成很多子问题从而得到状态转移方程。
子问题:不断分割,先求长度为1,就是一个字符的回文序列,也就是从i到i,显而易见花费为0,然后利用代码中的k值来表示字符串的长度,当k为1时,有很多状态,例如0...1,1...2,2...3  内层循环将这里状态变成回文序列进行求解最小花费。循环完毕,k++;
一直到求解出dp[0][m-1];(即是从0到m-1,长度为m,的回文序列,即题目要求的)
 
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[2200];
int dp[2000][2000];
int cost[2200];
int main()
{
int n,m;
cin>>n>>m;
scanf("%s",s);
char x;
int y=0,z=0;
for(int i=0;i<n;i++)
{
cin>>x>>y>>z;
cost[x-'a']=min(y,z);
}
for(int k=0;k<m;k++)//k代表的是i与j之间的距离
{
for(int i=0;i+k<m;i++)
{
int j=i+k;
dp[i][j]=min(dp[i][j-1]+cost[s[j]-'a'],dp[i+1][j]+cost[s[i]-'a']);
if(s[i]==s[j])
{
if(i==j||i+1==j) dp[i][j]=0;
else dp[i][j]=dp[i+1][j-1];
}
}
}
cout<<dp[0][m-1]<<endl;
return 0;
}

  

 
 
 

DP E - Cheapest Palindrome的更多相关文章

  1. POJ 题目3280 Cheapest Palindrome(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7148   Accepted: 34 ...

  2. 【POJ】3280 Cheapest Palindrome(区间dp)

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

  3. Cheapest Palindrome(区间DP)

    个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...

  4. POJ3280 Cheapest Palindrome 【DP】

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 29 ...

  5. 【POJ - 3280】Cheapest Palindrome(区间dp)

    Cheapest Palindrome 直接翻译了 Descriptions 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符 ...

  6. POJ 3280 Cheapest Palindrome DP题解

    看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...

  7. DP:Cheapest Palindrome(POJ 3280)

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

  8. POJ 3280 Cheapest Palindrome(DP)

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

  9. POJ 3280 Cheapest Palindrome(DP)

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

随机推荐

  1. Angular2入门:TypeScript的类型 - let , var, const

    一.let 二.const

  2. [JZOJ5836] Sequence

    Problem 题目链接 Solution 吼题啊吼题! 首先如何求本质不同的子序列个数就是 \(f[val[i]]=1+\sum\limits_{j=1}^k f[j]\) 其中 \(f[i]\) ...

  3. [PHP] 算法-两个n位的二进制整数相加问题PHP实现

    两个n位二进制数分别存储在两个n元数组A和B中,这两个整数的和存在一个n+1元的数组C中答:此问题主要是考察相加进位的问题,元素1+1 =0 并且往前进一位ADD-BINARY(A,B) C=new ...

  4. linq使用Take和Skip实现分页

    ;//第1页 ;//页大小 var list = list.Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();

  5. 初学HTML-8

    video标签:播放视频 格式一:<video src=""> </video> video标签的属性: src:用于告诉video标签需要播放的视频地址. ...

  6. Breeze 部署 Kubernetes 1.12.1高可用集群

    今天看文章介绍了一个开源部署 K8S 的工具,有空研究下~ Github 地址: https://github.com/wise2c-devops/breeze

  7. SQL Anywhere5.5: Metadata

    http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...

  8. Excel破解工作表保护

    宏运行 Public Sub Password_cracking() Const DBLSPACE As String = vbNewLine & vbNewLine Const AUTHOR ...

  9. HttpClient与浏览器调用服务接口差异

    我用httpclient访问接口,统计图有些不均匀,差距较大 ,有时只有几十毫秒,下图看到这种情况占多数,600-800毫秒之间的算是浏览器正常的产生调用接口的时间耗时 然后用jmeter跑时都是均值 ...

  10. Android、IOS文字居中偏离的解决方案

    前言 移动端开发,经常会遇到的问题,就是文字居中.一般都只能往css方向去fix这个问题. 自己以前也用过position:relative;top:-*px的方式去解决.