#include <iostream>
#include <cstdio>
#include <memory.h>
#include <vector>
#include <limits.h>
using namespace std;
vector<int>v,stak;
vector<vector<int> >ans;
int tar,start,minv,ansSum;
void dfs(int curi,int sum){
if(sum<=tar&&curi==v.size()){
if(tar-sum<minv){
ansSum=sum;
minv=tar-sum;
ans.clear();
ans.push_back(stak);
}else if(tar-sum==minv){
ans.push_back(stak);
}
}
if(sum>tar||curi>=v.size())return;
int subSum=0;
for (int i=curi;i<v.size();++i){
subSum=subSum*10+v[i];
stak.push_back(subSum);
dfs(i+1,subSum+sum);
stak.pop_back();
}
}
int main(){
while (scanf("%d%d",&tar,&start)){
if(!tar&&!start)break;
v.clear();
while (start){
v.insert(v.begin(),start%10);
start/=10;
}
minv=INT_MAX;
ansSum=INT_MAX;
ans.clear();
dfs(0,0);
if(ans.size()==0){
printf("error\n");
}else if(ans.size()>1){
printf("rejected\n");
}else{
printf("%d",ansSum);
for (int j=0;j<ans[0].size();++j){
printf(" %d",ans[0][j]);
}
printf("\n");
}
}
return 0;
}
ZOJ 1694 Shredding Company(简单DFS)
最新推荐文章于 2019-03-18 21:57:16 发布