【打CF,学算法——四星级】CodeForces 31E TV Game (动态规划)

【CF简介】

提交链接:CF 31E


题面:

E. TV Game
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

Description

There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactlyn moves. On it's turni-th player takes the leftmost digit of A and appends it to his or her numberSi. After that this leftmost digit is erased fromA. Initially the numbers of both players (S1 andS2) are «empty». Leading zeroes in numbersA, S1, S2 are allowed. In the end of the game the first player getsS1 dollars, and the second getsS2 dollars.

One day Homer and Marge came to play the game. They managed to know the numberA beforehand. They want to find such sequence of their moves that both of them makes exactlyn moves and which maximizes their total prize. Help them.

Input

The first line contains integer n (1 ≤ n ≤ 18). The second line contains integerA consisting of exactly2n digits. This number can have leading zeroes.

Output

Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactlyn moves. If there are several solutions, output any of them.

Sample Input

Input
2
1234
Output
HHMM
Input
2
9911
Output
HMHM


题意:

    给定1个2*n长的数字,从左端开始,分配给A,B两个空串的高位,要求最后A,B两个串的长度相同(允许前导0),且A,B两串转换为数字后,和最大。输出原串每一位的分配方案。


解题:

     采用dp的方法解决这道题,dp[i][j]表示前i位,分配给j个给A串,所能取得的两数的和的最大值。因为分配从高位到低位,确定了某位是什么,其在最终串中的位置也是确定的,故而可以直接乘以pow(10,n-i),i为当前分配的是左数第几位。

      这题自己被坑了好多次,想着让范围大一点,就用了ULL,后来发现全0会出错,就把初值赋为-1,而ULL无法表示-1,又调了好久,还有就是路径特意反了一下,结果反多了,MH反一下倒是无所谓。转移方程不难写,就是分配给A,B两种情况,限制条件要注意一下。


代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
LL dp[40][40]={0};
int chs[40][40];
char s[40];
int path[40];
LL Pow(int x)
{
	LL res=1;
	while(x--)
		res*=10;
	return res;
}
int main()
{
	int n,p1,p2;
	LL tmp;
	scanf("%d",&n);
	scanf("%s",s);
    for(int i=0;i<=2*n;i++)
		for(int j=0;j<=n;j++)
			dp[i][j]=-1;
	dp[1][0]=dp[1][1]=(s[0]-'0')*(Pow(n-1));
	chs[1][0]=0;
	chs[1][1]=1;
	for(int i=1;i<2*n;i++)
	{
	   int lim=min(i,n);
	   for(int j=0;j<=lim;j++)
	   {
		   if(n-i+j-1>=0)
		   {
			   tmp=dp[i][j]+(s[i]-'0')*Pow(n-i+j-1);

			   if(tmp>dp[i+1][j])
			   {
				   dp[i+1][j]=tmp;
				   chs[i+1][j]=0;
			   }
		   }
		   if(n-j>0)
		   {

		     tmp=dp[i][j]+(s[i]-'0')*Pow(n-j-1);

			 if(tmp>dp[i+1][j+1])
			 {
				 chs[i+1][j+1]=1;
				 dp[i+1][j+1]=tmp;

			 }
		  }
	   }
	}
	p1=2*n,p2=n;
    while(p1)
	{
       if(chs[p1][p2])
	   {
		   path[p1]=1;
		   p2--;
	   }
	   else
		   path[p1]=0;
	   p1--;
	}
	for(int i=1;i<=2*n;i++)
		if(path[i])
			printf("H");
	    else
			printf("M");
	printf("\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值