原题:
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7669 Accepted: 3723
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
USACO 2007 Open Gold
大意:
给你一个字符串,让你增加或减少一些字母,使得这个字符串变成回文串。然后给你一些你用到的字母,每个字母都有增加的费用和删除的费用。现在让你使用最小的代价使得这个字符串变成回文。
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
struct node
{
int add,del;
};
node cost[27];
int dp[2001][2001];
string s;
int n,len;
void ini()
{
for(int i=0;i<=26;i++)
cost[i].add=cost[i].del=-1;
for(int i=0;i<=2000;i++)
dp[i][i]=0;
}
int min3(int x,int y,int z)
{
return min(min(x,y),min(y,z));
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>len)
{
cin>>s;
char c;
int a,d;
ini();
for(int i=1;i<=n;i++)
{
cin>>c>>a>>d;
cost[c-'a'+1].add=a;
cost[c-'a'+1].del=d;
}
for(int l=2;l<=len;l++)
{
for(int i=0;i<=len-l+1;i++)
{
int j=i+l-1;
dp[i][j]=INT_MAX;
if(s[i-1]==s[j-1])
dp[i][j]=dp[i+1][j-1];
else
{
dp[i][j]=min3(dp[i][j-1]+cost[s[j-1]-'a'+1].add,dp[i][j-1]+cost[s[j-1]-'a'+1].del,dp[i][j]);
dp[i][j]=min3(dp[i+1][j]+cost[s[i-1]-'a'+1].add,dp[i+1][j]+cost[s[i-1]-'a'+1].del,dp[i][j]);
}
}
}
cout<<dp[1][len]<<endl;
}
return 0;
}
解答:
在挑战程序设计竞赛的练习题里面看到的这个题目,之前做过几个有关回文串的问题,所以这道题秒出了。
首先设置dp[i][j]表示第i个字母到第j个字母之间(实际上是第i-1个到第j-1,这里打字方便)的字符串形成回文串所花费的最小费用。
接下来考虑状态转移:
//比如现在要考虑第i到第j个字符之间形成回文串的最小费用
//YxxxxxxxZ 其中Y是第i个字符,Z是第j个字符
//i.......j
由于i到j-1和i+1到j是之前已经判断完成的状态,也就是i到j-1和i+1到j之间已经是形成回文串而且花费最小。
现在考虑由i到j-1状态转移成i到j状态,那么有两种方法使得i到j变成回文串。一种是把左侧的第i个字符删除,使得变成i+1到j-1的回文串。另一种是在第j个位置加上一个第i个字符。
同理第i+1到j的状态。
最后如果第i和第j个字母相同,那么明显不用花费任何代价。dp[i][j]=dp[i+1][j-1]即可。
最后转移方程为dp[i][j]=min(dp[i][j-1]+add[s[j]] , dp[i][j-1]+del[s[j]] ,
dp[i+1][j]+add[s[i]] , dp[i+1][j]+del[s[i]])
以上是字母s[i]和s[j]不相等的情况,如果s[i]==s[j] dp[i][j]=d[i+1][j-1]
其中s是字符串,add表示增加某个字母代价,del表示删除某个字符的代价。代码中的字符串下标是从0开始的,注意即可。