POJ 3280 Cheapest Palindrome 动态规划法题解

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 lengthM (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

一看这道题总觉得是字符串处理问题,其实是需要建模动态规划法的题解。

动态规划法的建模都感觉是最难的一关了,当然最简单是参考别人的,自己建模真的很难。

本题的建模就是利用一个二维数组palin[i][j],代表j个字符,就是如果字符串的起点下标为i,那么i到i+j-1字符的最小修改值是多少。

也可以用递归的思维从这个字符串一步一步往更小的字符串递推出来。

最终优化程序,使用滚动数组变二维数组维一维。

下面程序作出详细注解:

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <algorithm>
using namespace std;

const int MAX_M =  2001;
const int ALP_NUM = 26;
int palin[2][MAX_M];//palin[id][i],id数组代表当前d个字符段计算的最小修改值,!id数组代表d-1个字符段的计算值,那么如果id维的数组没有计算前,代表d-2个字符段计算的最小修改值
int cost[ALP_NUM];//只需要计算最小的修改值就可以了
char cow[MAX_M];//id字符串

int getMinCost(int M)
{
	memset(palin[0], 0, sizeof(int) * (M+1));
	memset(palin[1], 0, sizeof(int) * (M+1));//初始值都为零,只有一个字符的时候肯定是不需要修改的Palindrome,修改值为零
	bool id = 0;
	for (int d = 2; d <= M; d++)
	{
		id = !id;
		for (int i = 0, j = d-1; j < M; i++, j++)
		{
			//只需要统一记录方法,i记录当前d个字符的修改值
			//两个字符相等,那么直接等于d-2个字符的时候的修改值
			if (cow[i] == cow[j]) palin[id][i] = palin[id][i+1];
			else//两个字符不相等就比较处理这两个字符哪个便宜
			{
				//palin[!id][i+1]代表i+1到i+1+d-1个字符的处理值
				//故此不包含第i个字符
				int c1 = palin[!id][i+1] + cost[cow[i]-'a'];
				//palin[!id][i]代表i到i+d-1个字符的处理值
				//故此不包含第j个字符
				int c2 = palin[!id][i] + cost[cow[j]-'a'];
				//palin[id][i]代表i到i+d个字符的处理值
				palin[id][i] = min(c1, c2);
			}
		}
	}
	return palin[id][0];
}

int main()
{
	int N, M, a, d;
	while (scanf("%d %d", &N, &M) != EOF)
	{
		getchar();
		gets(cow);//gets后面处理掉\n字符了
		fill(cost, cost+ALP_NUM, 0);
		for (int i = 0; i < N; i++)
		{
			char ch = getchar();
			scanf("%d %d", &a, &d);//插入或者删除的处理字符代价
			cost[ch-'a'] = min(a, d);//只需要记录处理字符的最小代价就可以了
			getchar();//处理掉后面的\n字符
		}
		printf("%d\n", getMinCost(M));
	}
	return 0;
}
时间: 2024-10-10 09:44:39

POJ 3280 Cheapest Palindrome 动态规划法题解的相关文章

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

[poj]3280 Cheapest Palindrome 题解

[poj]3280 Cheapest Palindrome 区间dp 题意: 给你长度为m的字符串,其中有n种字符,每种字符都有两个值,分别是插入这个字符的代价,删除这个字符的代价,让你求将原先给出的那串字符变成一个回文串的最小代价. M<=2000 设 dp[i][j] 为区间 i~j 的回文串的最小代价 现在考虑怎样从别的状态转移到 区间i~j 三种情况 首先 str[i]==str[j] 那么 dp[i][j] = dp[i+1][j-1] 其次 (i+1)~j 是一个回文串 dp[i][

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

POJ 3280 Cheapest Palindrome(区间DP)

 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6894   Accepted: 3344 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 co

poj 3280 Cheapest Palindrome(区间DP)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5995   Accepted: 2922 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 a

POJ 3280 —— Cheapest Palindrome

题目:http://poj.org/problem?id=3280 经典的区间DP #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; char s[2005], ch[2]; int cost[26]; int dp[2005][2005]; int main () { int n, m, c1, c2; scanf(

POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[

(中等) 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

POJ 3280 Cheapest Palindrome

区间DP. dp[i][j]表示把区间[i,j]变成回文的最小花费. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; long long INF=999999999999; const int maxn=2000+10; int q,n; char s[maxn]; long long cost[30]; long long