Poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + pruning or weight determination)

Source: Internet
Author: User

Sum It UpTime Limit: 2000/1000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other) Total Submission (s): 4 Accepted Submission (s): 1 Font: times New Roman | Verdana | GeorgiaFont Size: Regular → Problem DescriptionGiven a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. for example, if t = 4, n = 6, and the list is [,], then there Re four different sums that equal 4: 4, 3 + 1, 2 + 2, and 2 + 1 + 1. (A number can be used within a sum as every times as it appears in the list, and a single number counts as a sum .) your job is to solve this problem in general. inputThe input will contain in one or more test cases, one per line. each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1 ,. .., Xn. if n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (random SIVE ), and x1 ,..., xn will be positive integers less than 100. all numbers will be separated by exactly one space. the numbers in each list appear in nonincreasing order, and there may be repetitions. outputFor each test case, first output a line containi Ng 'sums', the total, and a colon. then output each sum, one per line; if there is no sums, output the line 'none '. the numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as usual times as it was repeated in the original list. the sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. in other words, the sums mu St be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. within each test case, all sums must be distince; the same sum connot appear twice. sample Input4 6 4 3 2 1 15 3 2 1 1400 12 50 50 50 50 25 25 25 25 25 250 0 Sample OutputSums of + 12 + 22 + 1 + 1 Sums o F 5: NONESums of 400: 50 + 50 + 50 + 50 + 50 + 50 + 25 + 25 + 25 + 2550 + 50 + 50 + 50 + 50 + 25 + 25 + 25 + 25 + 25 + 25 + 25 Source during the fourth college program design competition of Zhejiang University of Technology, tangle! At that time, I had been thinking about how to judge the weight. I had YY a hash function WA... A solution on the Internet does not need to be judged to be duplicated, so it can be used for pruning. I retried it according to that idea. Code1: (dfs + pruning) poj: Accepted 388 K 0 ms g ++ 930B [cpp] # include <stdio. h> # include <string. h> int a [15]; int p [15]; int vis [15]; int t, n, flag; void dfs (int k, int sum) {int I; if (k> n | sum <0) return; if (sum = 0) {flag = 1; for (I = 0; I <K-1; I ++) printf ("% d +", p [I]); printf ("% d \ n", p [I]); return ;}for (I = k; I <n; I ++) if (! Vis [I]) {if (sum-a [I] <0 | (k> 0 & a [I]> p [k-1]) continue; vis [I] = 1; p [k] = a [I]; dfs (k + 1, sum-a [I]); vis [I] = 0; while (I + 1 <n & a [I] = a [I + 1]) I ++; // after the search is complete, if the number of the next search is still the same as the current one, search for the next one. {Deduplication }}int main () {int I; while (scanf ("% d", & t, & n), t + n) {for (I = 0; I <n; I ++) scanf ("% d", & a [I]); I = 0; while (I <n & a [I]> t) I ++; printf ("Sums of % d: \ n", t); flag = 0; memset (vis, 0, sizeof (vis); dfs (I, t); if (! Flag) printf ("NONE \ n");} return 0 ;}# include <stdio. h> # include <string. h> int a [15]; int p [15]; int vis [15]; int t, n, flag; void dfs (int k, int sum) {int I; if (k> n | sum <0) return; if (sum = 0) {flag = 1; for (I = 0; I <K-1; I ++) printf ("% d +", p [I]); printf ("% d \ n", p [I]); return ;}for (I = k; I <n; I ++) if (! Vis [I]) {if (sum-a [I] <0 | (k> 0 & a [I]> p [k-1]) continue; vis [I] = 1; p [k] = a [I]; dfs (k + 1, sum-a [I]); vis [I] = 0; while (I + 1 <n & a [I] = a [I + 1]) I ++; // after the search is complete, if the number of the next search is still the same as the current one, search for the next one. {Deduplication }}int main () {int I; while (scanf ("% d", & t, & n), t + n) {for (I = 0; I <n; I ++) scanf ("% d", & a [I]); I = 0; while (I <n & a [I]> t) I ++; printf ("Sums of % d: \ n", t); flag = 0; memset (vis, 0, sizeof (vis); dfs (I, t); if (! Flag) printf ("NONE \ n");} return 0;} code2: (deduplicated with set: Submit all hangs on POJ and ZOJ, but the hdu can be AC, er, Er ~) HDU: accepted 1258 0 MS 340 K 1286 B G ++ [cpp] # include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <functional> # include <vector> # include <string> # include <set> using namespace std; # define N 20 int t, n; int a [N]; int list [N]; bool vis [N]; set <string> s; bool flag; void dfs (int k, int sum) {int I; if (k> = n | sum <0) return; if (sum = 0) {string str; for (I = 0; I <k; I ++) {str + = (list [I]/10) + '0'; str + = (list [I] % 10) + '0 ';} if (s. find (str) = s. end () {s. insert (str); flag = 1; for (I = 0; I <K-1; I ++) printf ("% d +", list [I]); printf ("% d \ n", list [I]);} return ;}for (I = k; I <n; I ++) if (! Vis [I] & (k = 0 | a [I] <= list [k-1]) {vis [I] = 1; list [k] = a [I]; dfs (k + 1, sum-a [I]); vis [I] = 0 ;}} int main () {int I; while (scanf ("% d", & t, & n), t + n) {for (I = 0; I <n; I ++) {scanf ("% d", & a [I]);} memset (vis, 0, sizeof (vis); printf ("Sums of % d: \ n ", t); flag = false; s. clear (); dfs (0, t); if (! Flag) printf ("NONE \ n");} return 0 ;} # include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <functional> # include <vector> # include <string> # include <set> using namespace std; # define N 20int t, n; int a [N]; int list [N]; bool vis [N]; set <string> s; bool flag; void dfs (int k, int sum) {int I; if (k> = n | sum <0) return; if (sum = 0) {string str; for (I = 0; I <k; I ++) {str + = (list [I]/ 10) + '0'; str + = (list [I] % 10) + '0';} if (s. find (str) = s. end () {s. insert (str); flag = 1; for (I = 0; I <K-1; I ++) printf ("% d +", list [I]); printf ("% d \ n", list [I]);} return ;}for (I = k; I <n; I ++) if (! Vis [I] & (k = 0 | a [I] <= list [k-1]) {vis [I] = 1; list [k] = a [I]; dfs (k + 1, sum-a [I]); vis [I] = 0 ;}} int main () {int I; while (scanf ("% d", & t, & n), t + n) {for (I = 0; I <n; I ++) {scanf ("% d", & a [I]);} memset (vis, 0, sizeof (vis); printf ("Sums of % d: \ n ", t); flag = false; s. clear (); dfs (0, t); if (! Flag) printf ("NONE \ n");} return 0 ;}

Related Article

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.