zoukankan      html  css  js  c++  java
  • HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up【DFS】

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7926   Accepted: 4068

    Description

    Given 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 [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.

    Input

    The input will contain 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 x 1 , . . . , x n . 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 (inclusive), and x 1 , . . . , x n 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.

    Output

    For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are 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 many 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 must 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 distinct; the same sum cannot appear twice.

    Sample Input

    4 6 4 3 2 2 1 1
    5 3 2 1 1
    400 12 50 50 50 50 50 50 25 25 25 25 25 25
    0 0

    Sample Output

    Sums of 4:
    4
    3+1
    2+2
    2+1+1
    Sums of 5:
    NONE
    Sums of 400:
    50+50+50+50+50+50+25+25+25+25
    50+50+50+50+50+25+25+25+25+25+25
    

    Source




    Regionals 1997 >> North America - Mid-Central USA


    问题链接HDU1258POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up

    题意简述:参见上文。

    问题分析

    这个问题是给出一个和,给出若干个正整数,求其哪几组数相加等于指定的和。可以使用DFS来实现。

    关键是搜索过程中要去掉重复的解。

    DFS属于回溯法,在搜索过程中也可以使用已知条件进行剪枝以加快速度。

    程序说明:本程序中,当和的剩余部分小于最小整数值时,不是继续搜索而是回溯,可以加快搜索速度。


    AC通过的C语言程序如下:

    /* HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up */
    
    #include <stdio.h>
    #include <memory.h>
    
    #define MAXN 15
    
    int data[MAXN];
    int kcount[MAXN];
    int dsum[MAXN];
    int ans[MAXN];
    int kind;
    int residue;
    int count;
    
    void print_result()
    {
        int i, j, k;
        for(i=0, j=0; i<kind; i++) {
            for(k=1; k<=ans[i]; k++) {
                if(j == 0)
                    printf("%d", data[i]);
                else
                    printf("+%d", data[i]);
                j++;
            }
        }
        printf("
    ");
    }
    
    void dfs(int k)
    {
        int i;
    
        if(k == kind || dsum[k] < residue)
            return;
    
        for(i=kcount[k]; i>=0; i--) {
            residue -= i * data[k];
            if(residue < 0) {
                ;
            } else if(residue == 0) {
                ans[k] = i;
                count++;
                print_result();
                ans[k] = 0;
            }  else if(residue > 0 && residue >= data[kind-1]) {
                ans[k] = i;
                dfs(k+1);
                ans[k] = 0;
            }
            residue += i * data[k];
        }
    }
    
    int main(void)
    {
        int total, n, sum, i;
    
        while(scanf("%d%d", &total, &n) != EOF) {
            // 判定结束条件
            if(total == 0 && n == 0)
                break;
    
            // 读入数据,并求和
            sum = 0;
            for(i=0; i<n; i++) {
                scanf("%d", &data[i]);
                sum += data[i];
            }
    
            // 整理:同值合并
            for(i=1, kind=0, kcount[0]=1; i<n; i++) {
                if(data[i] == data[kind])
                    kcount[kind]++;
                else {
                    data[++kind] = data[i];
                    kcount[kind] = 1;
                }
            }
            kind++;
    
            // 计算总和:dsum[i]为第i类之后各个数的总和
            dsum[0] = sum;
            for(i=1; i<kind; i++)
                dsum[i] = dsum[i-1] - data[i-1] * kcount[i-1];
    
            // 输出第一行
            printf("Sums of %d:
    ", total);
    
            // 深度优先搜索,并输出结果
            count = 0;
            residue = total;
            memset(ans, 0, sizeof(ans));
            dfs(0);
    
            // 输出结果
            if(count == 0)
                printf("NONE
    ");
        }
    
        return 0;
    }


  • 相关阅读:
    PHP学习之字符串
    PHP学习之常量
    PHP之数据类型
    AngularJS学习之Select(选择框)
    Angular JS 学习之Http
    PHP之echo/print
    Angular JS 学习之服务(Service)
    Angular JS 学习之过滤器
    git打包
    gdb分析core文件
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564611.html
Copyright © 2011-2022 走看看