31E:TV Game
題意簡述
給出一個長度爲
要形成兩個長度爲
要使得
思路
先把
這樣有什麼好處呢…方便了轉移,因爲答案是兩個數的和。
如果正着轉移的話,要同時記錄兩個數?非常麻煩。
時間複雜度
代碼
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
long long ans;
int n,cnt;
char pre[20][20],ansf[40],st[40];
long long f[20][20],pow10[20];
int main()
{
scanf("%d",&n);
scanf("%s",st);
reverse(st,st+n*2);
pow10[0]=1;
for (int i=1;i<=n;i++)
pow10[i]=pow10[i-1]*10;
for (int i=0;i<=n;i++)
for (int j=0;j<=n;j++)
{
if (f[i][j]+pow10[i]*(st[i+j]-'0')>=f[i+1][j])
{
f[i+1][j]=f[i][j]+pow10[i]*(st[i+j]-'0');
pre[i+1][j]='H';
}
if (f[i][j]+pow10[j]*(st[i+j]-'0')>=f[i][j+1])
{
f[i][j+1]=f[i][j]+pow10[j]*(st[i+j]-'0');
pre[i][j+1]='M';
}
}
int x=n,y=n;
while (x||y)
{
if (pre[x][y]=='H')
{
ansf[++cnt]='H';
x--;
}
else
{
ansf[++cnt]='M';
y--;
}
}
for (int i=1;i<=cnt;i++)
printf("%c",ansf[i]);
return 0;
}