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 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
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
这题真想了很久,感觉无从下手。很容易知道可以利用动态规划解决,但就连添加或删除某字符这个操作都是很难的,更不要谈该如何添加或删除字符串使得成为一个回文字符串。但其实根本不需要实际操作这些。。。我们可以仔细分析一下回文字符串的特性,比如一个字符串"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,所以这个要特判一下。
#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;
}