POJ 3280 Cheapest Palindrome 经典回文串DP 回文串问题阶段性小结

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;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值