POJ1416 Shredding Company(dfs)

时间:2021-12-11 21:41:52

题目链接

分析:

这题从早上调到现在。也不算太麻烦,细节吧。

每个数字都只有两种状态,加入前一序列和不加入前一序列。DFS枚举。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <string>
#include <queue> using namespace std; int ord[], len, ans_a[], tar, max_ans, cnt;
char s[];
bool flag; void dfs(int cur_s, int cur_t, int cur) {
if(cur_s+cur_t > tar) { // error
return ;
}
else if(cur == len) { //
int t = cur_s + cur_t;
if(t > tar) return ;
else if(t > max_ans) {
cnt = ;
max_ans = t;
memcpy(ans_a, ord, sizeof(ord));
}
else if(t == max_ans) {
cnt++;
}
flag = true;
return ;
} ord[cur] = ;
dfs(cur_s+cur_t+s[cur]-'', , cur+); if(cur != len-) {
ord[cur] = ;
dfs(cur_s, (cur_t+s[cur]-'')*, cur+);
}
} int main() {
int n;
// freopen("my.txt", "r", stdin); while(scanf("%d %d", &tar, &n) == && (n | tar)) {
flag = false;
max_ans = ; cnt = ; sprintf(s, "%d", n);
len = strlen(s); dfs(, , ); if(!flag) printf("error\n");
else if(cnt >= ) printf("rejected\n");
else {
printf("%d", max_ans);
int sn=;
for(int i=; i<len; i++) {
if(ans_a[i] == ) {
printf(" %d", sn*+s[i]-'');
sn = ;
}
else {
sn = sn*+s[i]-'';
}
} putchar('\n');
}
} return ;
}