Shredding Company (HDU 1539 Dfs)

Source: Internet
Author: User

Shredding CompanyTime limit:5000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 445 Accepted Submission (s): 124


Problem Descriptionyou has just been put in charge of developing a new shredder for the shredding company. Although a "normal" shredder would just shred sheets of paper to little pieces so that the contents would become Unreada BLE, this new shredder needs to the following unusual basic characteristics.
The shredder takes as input a target number and a sheet of paper with a number written on it.
It shreds (or cuts) the sheet into pieces each of the which have one or more digits on it.
The sum of the numbers written on each piece are the closest possible number to the target number, and without going over it.
For example, suppose then the target number is, and the sheet of paper have the number 12346. The shredder would cut the sheet into four pieces, where one piece have 1, another have 2, the third has, and the fourth Has 6. This is because their sum (= 1 + 2 + + 6) are closest to the target number in all possible combinations without go ing over 50. For example, a combination where the pieces was 1, 4, and 6 is not valid, because the sum of this combination 34 (= 1 + + + 4 + 6) is less than the above combination ' s 43. The combination of the 6, the valid either, the because (= + + 6) is greater than the target number of 50.

There is also three special rules:

If the target number is the same as the number in the sheet of paper, then the paper was not cut. For example, if the target number is a and the number on the sheet of paper are also, then the paper are not cut.
If It isn't possible to do any combination whose sum are less than or equal to the target number and then error was printed on A display. For example, if the target number was 1 and the number on the sheet of paper is 123, it was not possible-make any valid C Ombination, as the combination with the smallest possible sum is 1, 2, 3. The sum for this combination are 6, which is greater than the target number, and thus error is printed.
If there is more than one possible combination where the sum was closest to the target number without going over it and then R Ejected is printed on a display. For example, if the target number is a, and the number on the sheet of paper was 111, then there is, possible Combinat Ions with the highest possible sum of: (a) 1 and one and (b) and 1; Thus rejected is printed.
In order to develop such a shredder, you are decided to first make a simple program that would simulate the above charact Eristics and rules. Given-Numbers, where the first is the target number and the second are the number on the sheet of paper-be shredded, You need the Shedder should "cut up" the second number.


Inputthe input consists of several test cases, each on one line, as follows:

T1 NUM1
T2 num2
. . .
TN numn
0 0

Each test case consists of the following-positive integers, which is separated by one space: (1) The first integer (t I above) is the target number; (2) The second integer (Numi above) is the number which is on the paper to be shredded.

Neither integers may have a 0 as the first digit, e.g., 123 are allowed but 0123 are not. Assume that bother integers is at the most 6 digits in length. A line consisting of zeros signals the end of the input.



Outputfor each test case in the input, the corresponding output takes one of the following three types:

Sum Part1 part2 ...
Rejected
Error
In the first type, PARTJ and sum has the following meaning:

Each partj are a number on one piece of shredded paper. The order of PARTJ corresponds to the order of the original digits on the sheet of paper.
Sum is the sum of the numbers after being shredded, i.e., sum = part1 + Part2 + ...
Each number should is separated by one space.

The message error is printed if it isn't possible to make any combination, and rejected if there are more than one POSSIBL E combination.

No extra characters including spaces is allowed at the beginning of all line and nor at the end of each line.


Sample Input
50 12346376 144139927438 92743818 33129 314225 1299111 33333103 8621506 11040 0

Sample Output
1 2 6283 144 139927438 92743818 3 3 12error21 1 2 9 9rejected103 2 0rejected

Sourceasia 2002, Kanazawa (Japan)
Recommendzl | We have carefully selected several similar problems for you:1849 1848 1847 1404 1517


Test instructions: Give a bunch of numbers to divide him into several parts so that their sum is less than or equal to the given goal, and as close as possible to goal.

Idea: DFS violence enumeration, note may not be more than 6 bits.

Code:

#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi  ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b;  i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d         ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;co NST int MAXN = 2005;const int MAXM = 200010;const int N = 1005;char str[maxn];int a[maxn],b[maxn];int goal,len;int Max;vector<int>ans,vec;vec  tor<vector<int> >out;void dfs (int pos,int sum) {if (sum+b[len]-b[pos-1]>goal) return;            The sum of the current sums plus all the remaining single numbers is bigger than goal. if (pos==len+1&&sum<=goal) {if (max<=sum) {            Ans.push_back (sum);            if (Out.size ()!=0) out.pop_back ();            Out.push_back (VEC);        Max=sum;    } return;        } for (int i=pos;i<=len;i++)//enumeration endpoint {int s=0;        for (int j=pos;j<=i;j++) {s=s*10+a[j];        } vec.push_back (s);        DFS (I+1,sum+s);    Vec.pop_back (); } return;    int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j;        while (scanf ("%d%s", &goal,str+1)} {if (goal==0&&str[1]== ' 0 ') break;        int x=0;        Memset (b,0,sizeof (b)); Len=strlen(str+1);            for (i=1;i<=len;i++) {a[i]=str[i]-' 0 ';            x=x*10+str[i]-' 0 ';        B[i]=b[i-1]+a[i];            } if (X==goal) {printf ("%d%d\n", goal,goal);        Continue            } if (B[len]>goal) {printf ("error\n");        Continue        } max=0;        Out.clear ();        Ans.clear ();        Vec.clear ();        DFS (1,0);        int sz=ans.size ();        if (Ans[sz-1]==ans[sz-2]) printf ("rejected\n");            else {printf ("%d", ans[sz-1]);            For (I=0;i<out[0].size (); i++) printf ("%d", out[0][i]);        printf ("\ n"); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shredding Company (HDU 1539 Dfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.