POJ - 3280 Cheapest Palindrome(区间dp)

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.

翻译

输入

第1行:两个空格分隔的整数:N和M

第2行:这一行正好包含M个字符,它们构成了初始ID字符串

第3…N+2行:每行包含三个空格分隔的实体:输入字母表的一个字符和两个整数,分别是添加和删除该字符的成本。
输出
求把所给字符串改成回文串的最低消费;

注意:个人觉得用递归写的代码更好理解

代码(dp)

#include "stdio.h"
#include "algorithm"
#include "string.h"
#include "math.h"
using namespace std;
int dp[2000][2000+9];
int a[200];
char b[2000+9];
int main()
{
    
    
	int n,i,j,k,l;
	char c;
	scanf("%d%d",&n,&l);
	scanf("%s",b);
	for(k=0; k<n; k++)
	{
    
    
		getchar();
		scanf("%c %d %d",&c,&i,&j);
		a[c]=min(i,j);//如ab,如果b为中心,要么加a变成:aba,要么删a变成:b,
		//效果一样的,就看加a和删a的价钱,故取最小值 
	}
	for(i=1; i<l; i++)
	{
    
    
		for(j=i; j>=0; j--)
		{
    
    
			if(b[i]==b[j])
				dp[i][j]=dp[i-1][j+1];
			else
				dp[i][j]=min(dp[i][j+1]+a[b[j]],dp[i-1][j]+a[b[i]]);
			//abc,从开始 i=3,修改b的最小值为a[b],修改c的最小值为a[c] 
			// j=3,c==c,dp[3][3]=0
			//j=2,b!=c,bc=dp[3][2]=(c+a[b],b+a[c])=(bc+a[b],bc+a[c])
			//c+a[b]=bc+a[b]:bc-->bcb or bc-->c,两种选着取决于a[b]中的的值为加b或删b 
			//b+a[c]=bc+a[c]:bc-->cbc or bc-->b,如上同 
		}
	}
	printf("%d\n",dp[l-1][0]);

}

代码(递归)

#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;
#define inf 0X3f3f3f
char a[2009];//数组 
int dp[2009][2009],c[300]; 
int dfs(int k,int j)
{
    
    
	
	if(k>=j)
		return dp[k][j]=0;
	if(dp[k][j]<inf)
		return dp[k][j];
	if(a[k]==a[j])
		return dp[k][j]=dfs(k+1,j-1);
	if(a[k]!=a[j])
		return  dp[k][j]=min(dfs(k+1,j)+c[a[k]],dfs(k,j-1)+c[a[j]]);
}
int main()
{
    
    
	int n,m;
	scanf("%d%d",&n,&m);
	scanf("%s",a);
	memset(dp,inf,sizeof(dp));
	for(int i=1;i<=n;i++)
	{
    
    
		getchar();
		char c1;int n1,n2;
		scanf("%c%d%d",&c1,&n1,&n2);
		c[c1]=min(n1,n2);
	}
	printf("%d\n",dfs(0,m-1));
 } 

猜你喜欢

转载自blog.csdn.net/weixin_53623850/article/details/120608599