Poj 1416 Shredding Company (dfs)

Source: Internet
Author: User

Link: poj 1416

A new shredder is used to cut the number on the paper into several parts,

Calculate the value that is closest to the target after the split and does not exceed the target value.

For example, if the target value is 50, and the number on the paper is 12346, you should cut the number into four parts,

They are 1, 2, 34, and 6. The sum of 43 (= 1 + 2 + 34 + 6) is the largest sum of up to 50.

For example, the sum of values 1, 23, 4, and 6 is 34, which is smaller than 43,

However, 12, 34, and 6 cannot be used because the sum of them exceeds 50.

There are three requirements for broken paper:

1. If the target value is equal to the value on the paper, it cannot be switched.
2. If there is no way to cut the number on the paper to a value not greater than the target value, an error is output.

For example, if the value of target is 1 and the number on the paper is 123, the sum you get is greater than 1.
3. If more than one method is used to obtain the optimal value, the rejected is output.

For example, if the value of target is 15 and the number on the paper is 111, the following two cut methods are available: 11, 1, or 11.

If there is only one optimal solution, the maximum value and the cutting scheme are output.

Train of Thought: use dfs to search for every possible situation, take a solution that does not exceed the maximum value of target, and record the division


# Include <stdio. h> # include <string. h> int ans, m, n, vis [1000000], step [10], now [10], num; char s [10]; void dfs (int pos, int sum, int cnt) {int I, t; if (pos> = m) {vis [sum] ++; if (sum> ans) {ans = sum; // record the optimum cut sum with num = cnt; for (I = 0; I <cnt; I ++) // record the current best solution step [I] = now [I];} return;} t = 0; for (I = pos; I <m; I ++) {t = t * 10 + s [I]-'0'; if (sum + t> n) return; now [cnt] = t; dfs (I + 1, sum + t, cnt + 1) ;}} int main () {int I, sum; while (scanf ("% d % s" , & N, s )! = EOF) {m = strlen (s); if (n = 0 & s [0] = '0' & m = 1) break; sum = 0; for (I = 0; I <m; I ++) sum + = s [I]-'0'; if (sum> n) {// if the smallest sum is greater than targe, it is certainly impossible to cut out the sum of printf ("error \ n"); continue;} memset (vis, 0, sizeof (vis); num = ans = 0; dfs (0, 0); if (vis [ans]> 1) printf ("rejected \ n "); else {printf ("% d", ans); for (I = 0; I <num; I ++) printf ("% d", step [I]); printf ("\ n") ;}} return 0 ;}


Poj 1416 Shredding Company (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.