poj 3280 Cheapest Palindrome DP

题意:给你一个m位的字符串,其中的字符是给定的n个小写字母之一,然后再字符串中添加或删除这字母的代价

   不一样。求在添加删除操作之后的字符串成为回文串的最小代价。

思路:考虑d[i][j]为字符串i到j成为回文串的最小代价。

   如Xx.....yY,如果X == Y,显然d[i][j] = d[i+1][j-1];

   如果X != Y,那么有4种方案:删除X,在Y后面添加一个X,删除Y,在X前面添加Y。从而转移方程可以写成

   d[i][j] = min(d[i+1][j]+del[s[i]-‘a‘], d[i+1][j]+add[s[i]-‘a‘], d[i][j-1] + del[s[j]-‘a‘], d[i][j-1] + add[s[j]-‘a‘]);

   由上面的方程可以分析得,对于删除添加字符s[i],我们只用取得其中最小的代价的操作就可以了,从而方程就更短了

   如d[i][j] = min(d[i+1][j]+cost[s[i]-‘a‘], d[i][j-1] + cost[s[j]-‘a‘]);

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int M = 2005;
int n,m,ch[2],cost[27],d[M][M];
char s[M];
void solve()
{
	memset(d, 0, sizeof(d));

	for(int dis = 1; dis < m; dis++)
	{
		for(int i = 0, j = i+dis; j < m; i++,j++)
		{
			if(s[i] == s[j]) d[i][j] = d[i+1][j-1];
			else{
				d[i][j] = min(d[i+1][j] + cost[s[i]-‘a‘], d[i][j-1] + cost[s[j]-‘a‘]);
			}
		}
	}
	printf("%d\n", d[0][m-1]);
}
int main()
{
	while(~scanf("%d %d", &n, &m))
	{
		memset(cost, 0, sizeof(cost));
		scanf("%s", s);
		for(int i = 0; i < n; i++)
		{
			int t1,t2;
			scanf("%s %d %d", ch,&t1, &t2);
			cost[ch[0]-‘a‘] = min(t1,t2);
		}
		solve();
	}
	return 0;
}
时间: 2024-10-12 21:21:18

poj 3280 Cheapest Palindrome DP的相关文章

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(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 题解

[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)

 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(区间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)

 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

[区间dp] poj 3280 Cheapest Palindrome

题意: 给出增加或减少某个字符的代价. 给你一个串,求让它变成回文串的最小代价. 思路: 和求次数一样. 然后注意的是其实增加和减少的性质是一样的.所以对于每个字符,取修改代价最小的就行了. 意思就是取增加和减少的最小值. 其他就同求次数的区间dp了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"q