[英]What is a fast way to find two sets with equal sums in C++?
I need an efficient way to look through a list of sets of ints
to see any two sets have the same sum. 我需要一种有效的方法来查看一组
ints
集,以查看任意两个ints
集具有相同的总和。 Specifically, in this case, I am generating the powerset of a list of ints that are given as an argument. 具体来说,在这种情况下,我正在生成作为参数给出的整数列表的幂集。 From that powerset, I need to find a pair of subsets that sum to the same value.
从该功率集,我需要找到总和为相同值的一对子集。
Currently, I'm trying to use a dynamic programming approach (explanation below): 当前,我正在尝试使用动态编程方法(下面进行解释):
typedef std::vector<int> intvec;
typedef std::vector<std::vector<int> > intvecvec;
typedef std::multimap<int,int> intmap;
/* Functions previously shown here that were removed:
* intvecvec search_for_sum(intvec num_list)
* and
* int sum_exists(std::vector<intvec> pset, int index, intmap &sums)
*/
/* This function was previously just called powerset(), and wasn't
* shown because the problem wasn't happening here. After refactoring,
* I removed the functions that were shown here previously and simply
* iteratively checked for sum matches while generating the powerset
*/
intvecvec powerset_search(intvec num_list)
{
intvecvec result;
std::multimap<int, intvec> power_set;
for (int c = 0; c < pow(2, num_list.size()); c++) {
intvec temp;
for (int i = 0; i < num_list.size(); i++) {
if (c & (1 << i)) {
temp.push_back(num_list.at(i));
}
}
int sum = std::accumulate( temp.begin(), temp.end(), 0 );
std::multimap<int, intvec>::iterator it = power_set.find(sum);
if (it == power_set.end()) {
power_set.insert(std::make_pair(sum, temp));
} else {
result.push_back(it->second);
result.push_back(temp);
break;
}
}
return result;
}
search_for_sum()
creates the powerset of the given list of ints
, along with a multimap
called sums. search_for_sum()
创建给定的ints
列表的幂集,以及一个称为sums的多重multimap
。 For every element in the powerset, its sum is calculated. 对于幂集中的每个元素,都会计算其总和。 If that sum has not yet been encountered, the sum and the current index is inserted into
sums
. 如果尚未达到该总和,则将总和和当前索引插入到
sums
。 Otherwise, the current set and the one that had already been inserted into sums
is returned. 否则,将返回当前集和已经插入到
sums
那个。
This works. 这可行。 The problem is that for large sizes of
num_list
, this can take a few minutes to come back with a solution if there is one. 问题是,对于大号
num_list
,如果有解决方案,可能要花几分钟才能找到解决方案。 This is significantly slower than the brute force method of just doing a double for
-loop and calculating sums each time to find a match. 这比只是做了双重强力法显著较慢
for
-loop和计算总和每次找到一个匹配。 Is there a better way for me to do this? 我有更好的方法吗?
EDIT: So I was able to solve this by moving my sum-checking step up to when the powerset is actually being generated, iteratively checking all previously entered sums and returning if a match was found. 编辑:因此,我能够通过将总和检查步骤移至实际生成幂集来解决此问题,迭代检查所有先前输入的总和并返回是否找到匹配项。 But, as requested in the comments, I have also redone the problem description to (hopefully) remove the vagueness that was there initially.
但是,按照评论中的要求,我还重做了问题描述,以(希望)消除最初存在的模糊性。
通过将总和检查步骤移至实际生成幂集的过程,可以迭代检查所有先前输入的总和并返回是否找到匹配项,从而解决了这一问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.