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 ;
}

【HDOJ】1539 Shredding Company的更多相关文章

  1. 【lightoj-1039】A Toy Company(BFS)

    The toy company "Babies Toys" has hired you to help develop educational toys. The current ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. java——递归调用

    递归函数调用调用本身,并通过自己的相应参数,这个计算过程中进行层,直到满足某些条件,只要停止呼叫. 递归函数的特点 1.函数要直接或间接调用自身. 2.要有递归终止条件检查.即递归终止的条件被满足后. ...

  2. iOS工具种之16进制颜色转为UIColor

     #define DEFAULT_VOID_COLOR [UIColor whiteColor] + (UIColor *)colorWithHexString:(NSString *)stringT ...

  3. Percona Live 2016 PPT整理-zhaiwx_yinfeng

    https://yq.aliyun.com/articles/54454?spm=5176.100239.bloglist.32.CllwIr

  4. [转] 有趣的JavaScript原生数组函数

    在JavaScript中,可以通过两种方式创建数组,Array构造函数和 [] 便捷方式, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typeof操作符返回‘obj ...

  5. Java基础知识强化之集合框架笔记12:Collection集合存储字符串并遍历

    1.  Collection集合存储字符串并遍历 分析: (1)创建集合对象 (2)创建字符串对象 (3)把字符串对象添加到集合中 (4)遍历集合 2. 代码示例: package cn.itcast ...

  6. yii2 控制器里 action 大小写组合造成的路由问题

    yii1中, 若存在如下控制器 class BindController extends CController { public function actionGetMobilePhone () { ...

  7. 洛谷 1373 小a和uim之大逃离

    /* 很容易想到f[i][j][k][l][01] 表示到ij点 两个人得分为kl 01表示这一步谁走的 因为起点不同 路径不同 所以要枚举起点.. 时间复杂度 O(nmk*nmk) 空间复杂度 O( ...

  8. Swift - 19 - 字典的初始化

    //: Playground - noun: a place where people can play import UIKit // 注意: swift中的字典用的也是中括号, 和OC的大括号是不 ...

  9. Java中final关键字的用法

  10. 【USACO 2.4.5】分数化小数

    [描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...