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.
题目大意:
给定字符串S、字符串的长度M以及S所含有的字符种数N(最多26种小写字母),然后给定这N种字母Add或Delete的代价,求将S变为回文串的最小代价和。
解题思路:
首先我们要先明确,对于设法构成回文串的问题,删除操作和增加操作其实是等价的(在某一侧增加一个字符等价于在另一侧删去相同的字符),这里我们考虑删除操作。
本题目的原始模型是通过增加或者删除某些字母将一个字符串变为回文串,求最少的操作次数。对于这个模型通常有两种解法:
1、将s翻转得到s’ ,然后使用动态规划求得两者最长公共子序列的长度l,最后用s的长度减去l得到答案;
2、用dp[ i ][ j ] 表示[ i, j]区间中子串的最优解,则:
若s[i] == s[j],则dp[i][j] = dp[i+1][j-1]
若s[i] != s[j],则dp[i][j] = min(dp[i+1][j],dp[i][j-1]) + 1
因为s[i]!=s[ j ],所以必定要删去左右边界中的某一个才能变成一个回文串,结果取 dp[i+1][j] 和 dp[i][j-1] 的较小值,然后再加上本次的操作。
下面再来看本题的模型,将增加和删除操作赋予了权重,那么根据之前的分析我们其实只需要保存两者中的较小值便可。上文提到的第一种方法已经不再适用,因此我们考虑第二种方法:
if (map[i] == map[j]) {dp[i][j] = dp[i + 1][j - 1];}
else {dp[i][j] = min(dp[i + 1][j] + s[i].cost, dp[i][j - 1] + s[j].cost);}
AC代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct node {
char ch; int cost;
}list[27];
char map[2005];
int dp[2005][2005];
int n, m, add, del;
int main() {
while (cin >> n >> m) {
for (int i = 1; i <= m; i++) { cin >> map[i]; }
for (int i = 1; i <= n; i++) {
cin >> list[i].ch >> add >> del;
list[i].cost = min(add, del);//删除和增加操作是等价的,只需要保存两者中的较小值便可
}
//初始化
for (int i = 0; i <= m; i++) {
for (int j = i; j >= 0; j--) {
dp[i][j] = 0;
}
}
for (int i = m-1; i >= 1; i--) {
for (int j = i+1; j <= m; j++) {
if (map[i] == map[j]) {
dp[i][j] = dp[i + 1][j - 1];
}
else {
int l, r;
for (l = 1; l <= n; l++) {
if (list[l].ch == map[i]) { break; }
}
for (r = 1; r <= n; r++) {
if (list[r].ch == map[j]) { break; }
}
dp[i][j] = min(dp[i + 1][j] + list[l].cost, dp[i][j - 1] + list[r].cost);
}
}
}
cout << dp[1][m] << endl;
}
}
题解过程中参考的大神博客:
(经典)POJ-3280 回文串DP
————————————————————————————————
参考了大神们的代码之后,发现可以直接使用一个int类型数组来保存花费,映射关系为cost[tmp-'a']
修改后的AC代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int list[27];
char map[2005];
int dp[2005][2005];
int n, m, add, del; char tmp;
int main() {
while (cin >> n >> m) {
for (int i = 1; i <= m; i++) { cin >> map[i]; }
for (int i = 1; i <= n; i++) {
cin >> tmp >> add >> del;
list[tmp - 'a'] = min(add, del);
}
//初始化
for (int i = 0; i <= m; i++) {
for (int j = i; j >= 0; j--) {
dp[i][j] = 0;
}
}
for (int i = m-1; i >= 1; i--) {
for (int j = i+1; j <= m; j++) {
if (map[i] == map[j]) {
dp[i][j] = dp[i + 1][j - 1];
}
else {
dp[i][j] = min(dp[i + 1][j] + list[map[i] - 'a'], dp[i][j - 1] + list[map[j] - 'a']);
}
}
}
cout << dp[1][m] << endl;
}
}