【HDOJ】1539 Shredding Company

时间:2021-01-04 08:16:56

DFS。

 /* 1539 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std; #define INF 0xfffffff int Q[];
char s[];
int ans;
int t, len;
bool flag;
vector<int> tb; void dfs(int beg, int sum, int n) {
int i, j, k, tmp; if (beg == len) {
if (sum<=t && sum>ans) {
tb.clear();
for (i=; i<n; ++i)
tb.push_back(Q[i]);
ans = sum;
flag = false;
n = ;
} else if (sum == ans) {
flag = true;
}
return ;
} for (i=beg; i<len; ++i) {
tmp = ;
for (j=beg; j<=i; ++j)
tmp = *tmp+s[j]-'';
Q[n] = tmp;
dfs(i+, sum+tmp, n+);
}
} int main() {
int i, j, k, tmp;
int sum; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %s", &t, s) != EOF) {
if (t== && s[]=='' && s[]=='\0')
break;
sum = ;
tmp = ;
for (i=; s[i]; ++i) {
tmp = *tmp+s[i]-'';
sum += s[i]-'';
}
len = i;
if (sum > t) {
printf("error\n");
continue;
}
if (tmp == t) {
printf("%d %s\n", t, s);
continue;
}
flag = false;
ans = -;
tb.clear();
dfs(, , );
if (flag) {
printf("rejected\n");
} else {
printf("%d", ans);
for (i=; i<tb.size(); ++i)
printf(" %d", tb[i]);
printf("\n");
}
} return ;
}