HDU-2476 String painter

挺有难度的一道题,区间DP,我参考别人的代码,自己加了写注释
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=100+10;
const int INF=0x7f7f7f7f;
char a[N],b[N];
int d[N][N],ans[N];
int main()
{
	while(~scanf("%s%s",a,b))
	{
		int l=strlen(b);
		memset(d,0,sizeof(d));
		for(int i=0;i<l;i++)
			d[i][i]=1;
		//先求将一个空串转换成b需要的最少步数,d[l][r]代表l~r区间
		for(int i=1;i<l;i++)
			for(int j=0;j+i<l;j++)
			{//如果左右端相同,可以开始直接将整个区间刷为一色,不需要再加一步
				d[j][j+i]=d[j+1][j+i]+(b[j]==b[j+i]?0:1); 
				for(int k=j+1;k<j+i;k++) //遍历一遍区间
					if(b[j]==b[k]) //如果和左端相同,可以考虑拆分为两个区间,可能减少步数
						d[j][j+i]=min(d[j][j+i],d[j+1][k]+d[k+1][j+i]);
			}
		//ans[i]代表将前i位a转换成b需要的步数
		ans[0]=(a[0]==b[0]?0:1); //第一位相同则ans[0]为0
		for(int i=1;i<l;i++)
		{
			ans[i]=d[0][i]; //初始值为将空串转化为b串0~i的步数
			if(a[i]==b[i]) //如果这一位相同,可以不管,是前一位的步数
				ans[i]=min(ans[i],ans[i-1]);
			for(int j=0;j<i;j++) //遍历区间0~i,考虑把前j+1~i为作为空串处理
				ans[i]=min(ans[i],ans[j]+d[j+1][i]);
		}

		printf("%d\n",ans[l-1]);
	}
	return 0;
}

Painter

08-05

The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of any three different colors, you get X ml of gray. (The paints are thick and "airy", almost like cake frosting, and when you mix them together the volume doesn't increase, the paint just gets more dense.) None of the individual colors are gray; the only way to get gray is by mixing exactly three distinct colors, but it doesn't matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class.nInputnnThe input consists of one or more test cases, followed by a line containing only zero that signals the end of the input. Each test case consists of a single line of five or more integers, which are separated by a space. The first integer N is the number of different colors (3 <= N <= 12). Following that are N different nonnegative integers, each at most 1,000, that specify the amount of each color needed. Last is a nonnegative integer G <= 1,000 that specifies the amount of gray needed. All quantities are in ml. nOutputnnFor each test case, output the smallest number of fingerpainting kits sufficient to provide the required amounts of all the colors and gray. Note that all grays are considered equal, so in order to find the minimum number of kits for a test case you may need to make grays using different combinations of three distinct colors.nSample Inputnn3 40 95 21 0n7 25 60 400 250 0 60 0 500n4 90 95 75 95 10n4 90 95 75 95 11n5 0 0 0 0 0 333n0nSample Outputnn2n8n2n3n4

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭