1 条题解

  • 0
    @ 2025-2-14 21:20:42

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int maxn=30;
    long long f[maxn][maxn],sum[maxn][maxn],g[maxn][maxn]={0};
    long long t,m,len;
    string s;
    void print(int,int);
    void work();
    int my_min(int,int);
    int main()
    {
    	//freopen("separate5.in","r",stdin);
    	//freopen("separate5.out","w",stdout);
    	work();
    	return 0;
    }
    void work()
    {
    	cin>>t;
    	for(int k=1;k<=t;k++)
    	{
    		cin>>s>>m;
    		//cout<<s<<' '<<m<<endl;
    		len=s.size();
    		for(int i=1;i<=len;i++)
    		{
    			long long tot=0;
    			for(int j=i;j<=len;j++)
    			{
    				tot=tot*10+s[j-1]-48;
    				sum[i][j]=tot;
    				//cout<<i<<' '<<j<<' '<<tot<<endl;
    			}
    		}
    		for(int i=0;i<=len;i++)
    			for(int j=0;j<=len;j++)f[i][j]=-1;
    		f[0][0]=1;
    		for(int i=1;i<=len;i++)
    		{
    			for(int j=1;j<=my_min(i,m);j++)
    			{
    				for(int k=1;k<=i;k++)
    				{
    					if(f[k-1][j-1]*sum[k][i]>f[i][j])
    					{
    						f[i][j]=f[k-1][j-1]*sum[k][i];
    						g[i][j]=k-1;
    					}
    				}
    			}
    		}
    		cout<<f[len][m]<<endl;
    		print(len,m);
    		cout<<endl;
    	}
    }
    int my_min(int x,int y)
    {
    	if(x<y)return x;
    	else return y;
    }
    void print(int len,int m)
    {
    	if(m==0)return;
    	print(g[len][m],m-1);
    	cout<<sum[g[len][m]+1][len]<<' ';
    }
    
    • 1

    信息

    ID
    832
    时间
    2000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者