2

我的目标是找到总和为给定总数的所有可能组合。例如,如果数组是 2 59 3 43 5 9 8 62 10 4 并且总数是 12,那么可能的组合是

2 10
3 9
8 4
5 3 4

这是我编写的第一组代码。想知道可以对此进行的最佳改进。

   int find_numbers_matching_sum(int *number_coll, int total)
{

    int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total);
    int location = search_till - number_coll;
    if (*search_till > total && location > 0 )
    {
        --location;
    }

    while ( location >= 0 )
    {
        find_totals(number_coll,total,location);
        --location;
    }
    return 1;
}

int find_totals(int *number_coll, int total, int end_location)
{
    int left_ones = total - number_coll[end_location];
    int curloc = end_location;
    int *search_till = 0;
    int location ;
    int all_numbers[10];
    int i = 0;

    all_numbers[i] = number_coll[end_location];
    while ( left_ones && curloc >= 0 )
    {
        search_till = lower_bound(number_coll,number_coll+end_location, left_ones);
        location = search_till - number_coll;
        if (*search_till > left_ones && location > 0 )
        {
            --location;
        }
        else if ( left_ones < *search_till )
        {
            break;
        }
        curloc=location;
        left_ones = left_ones - number_coll[curloc];
        all_numbers[++i] = number_coll[curloc];
        end_location = curloc - 1;
    }

    if ( !left_ones )
    {
        while ( i>=0)
        {
            cout << all_numbers[i--] << ' ';
        }
    }
    cout << endl;
    return 1;


}
4

6 回答 6

6

您描述的问题也称为NP-complete的子集和问题。您可以实现的最好的是指数时间算法,它尝试您的数组/集合的所有可能子集。

于 2010-07-21T19:07:50.740 回答
5

这是子集和问题(NP-Complete),直到 P ?= NP,只有指数解。

来自xkcd

于 2010-07-21T19:12:57.377 回答
3

这是NP 完全 背包问题子集和问题)的变体。完整的背包问题可以通过连续降低 N 线性减少到您的问题。如果 P != NP 成立,您将找不到比 N 中的指数运行更快的问题的精确算法。

然而,多项式时间近似是已知的。

于 2010-07-21T19:06:31.413 回答
3

如果值不大,比如你的总和以 M 为界,你可以使用动态规划。假设有 N 个项目。

想象一下你有一个矩阵DP[M][N]。单元格的DP[m][n]意思是:前 n 个元素的多少个组合恰好等于 m?

分析每个项目,您可能会或可能不会将其包含在某种组合中。然后你得到重复(照顾超出范围的值)

DP[m][n] = DP[m][n-1] + DP[m - v[n]][n - 1]

rhs 的第一项意味着您正在考虑所有不使用第 n 项的总和,第二项表示您正在考虑所有使用第 n 项的总和。你从基础开始DP[0][0] = 1,因为空集是一个有效的组合,总和为 0。所需的值在 DP[M][N] 中。

虽然这是伪多项式,O(MN).

于 2010-07-21T19:23:33.933 回答
3
#include <iostream>
#include <vector>

using namespace std;

struct State {
    int v;
    const State *rest;
    void dump() const {
        if(rest) {
            cout << ' ' << v;
            rest->dump();
        } else {
            cout << endl;
        }
    }
    State() : v(0), rest(0) {}
    State(int _v, const State &_rest) : v(_v), rest(&_rest) {}
};

void ss(int *ip, int *end, int target, const State &state) {
    if(target < 0) return; // assuming we don't allow any negatives
    if(ip==end && target==0) {
        state.dump();
        return;
    }
    if(ip==end)
        return;
    { // without the first one
        ss(ip+1, end, target, state);
    }
    { // with the first one
        int first = *ip;
        ss(ip+1, end, target-first, State(first, state));
    }
}

int main() {
    int a[] = { 2,59,3,43,5,9,8,62,10,4 };
    int * start = &a[0];
    int * end = start + sizeof(a) / sizeof(a[0]);
    ss(start, end, 12, State());
}
于 2010-07-21T21:13:46.067 回答
1

这与数论中的分区有关,可以使用动态规划来解决。

让我们n成为总数。让我们parts成为元素列表。我们假设它们是正整数。

if parts == []
then f(n,parts) = [] 
else let parts = x::queue and f(n,parts) = union(L1, L2)

where:

L1 = f(n, queue)

if n-x>0
then let result = f(n-x, queue) and L2 = concatenation([x], result)
else if n-x==0, L2 = [x]
else L2 = []

这是典型的家庭作业。

于 2010-07-21T19:28:34.263 回答