AT4298 [ABC118D] Match Matching
思路
这道题是典型的完全背包问题,需要求的是最大数字,而数字中的每一位需要的火柴数之和是一定的,所以要排序把大的数放在前面。
long long 在这道题肯定会超大小,最大会有
代码
#include<bits/stdc++.h>
using namespace std;
string dp[10010];
struct node{
int w,v;
}a[100010];
char add(int a,int b)//增加的字符
{
int c=a+b;
return char(c);
}
bool cmp(node x_,node _x)
{
return x_.w>_x.w;
}
string ma(string a,string b)//比较数字大小
{
if(a.size()>b.size())//长度比较
return a;
else
if(a.size()<b.size())
return b;
else
{
for(int i(0);i<a.size();++i)//按每个字符比较
if(a[i]>b[i])
return a;
else
if(b[i]>a[i])
return b;
return a;
}
}
int main()
{
int n,m,tot=0;
scanf("%d%d",&n,&m);
for(int i(1);i<=m;++i)
{
int x;
scanf("%d",&x);
a[++tot].w=x;
if(x==1)//记录在组成的数字需要的火柴数
a[tot].v=2;
if(x==2||x==3||x==5)
a[tot].v=5;
if(x==4)
a[tot].v=4;
if(x==6||x==9)
a[tot].v=6;
if(x==7)
a[tot].v=3;
if(x==8)
a[tot].v=7;
}
sort(a+1,a+n+1,cmp);//排序数字
dp[0]="";
for(int i(1);i<=tot;++i)
{
for(int j(a[i].v);j<=n;++j)
{
char x=add(a[i].w,'0');
if(dp[j-a[i].v]!=""||j-a[i].v==0)//判断能不能加
dp[j]=ma(dp[j],dp[j-a[i].v]+x);
}
}
printf("%s\n",dp[n].c_str());
return 0;
}