我找到了类似问题here 的答案,唯一的区别是我想要所有子集,而它们只需要最大长度为 2 的子集。
解决方案相当于找到所有可能的整数组合,总和为 n(输入列表的长度),然后将解决方案重新映射到单词列表以找到它的子集。
他们的问题的伪代码:
push an empty list onto the stack;
while (the stack is not empty) {
pop the top list off the stack;
if (the sum of its entries is n)
add it to the solution set;
else if (the sum of its entries is less than n)
add a 1 to a copy of the list and push it onto the stack;
add a 2 to a copy of the list and push it onto the stack;
}
}
这个问题的伪代码(扩展):
push an empty list onto the stack;
while (the stack is not empty) {
pop the top list off the stack;
if (the sum of its entries is n)
add it to the solution set;
else if (the sum of its entries is less than n)
for j = 1:n {
add j to a copy of the list and push it onto the stack;
}
}
}
我的 Python 实现:
import copy
def generate_subsets(words):
# get length of word list
list_len = len(words)
# initialize stack, subset_lens list
stack = [[], ]
subset_lens = []
while stack:
current_item = stack.pop(-1)
if sum(current_item) == list_len:
subset_lens.append(current_item)
elif sum(current_item) < list_len:
for j in range(1, list_len+1):
new_item = copy.deepcopy(current_item)
new_item.append(j)
stack.append(new_item)
# remap subset lengths to actual word subsets
subsets = []
for subset_len in subset_lens:
subset = []
starting_index = 0
for index in subset_len:
subset.append('_'.join(words[starting_index:starting_index+index]))
starting_index+= index
subsets.append(subset)
return subsets
输入:
generate_subsets(['AA', 'BB', 'CC', 'DD'])
输出:
['AA_BB_CC_DD']
['AA_BB_CC', 'DD']
['AA_BB', 'CC_DD']
['AA_BB', 'CC', 'DD']
['AA', 'BB_CC_DD']
['AA', 'BB_CC', 'DD']
['AA', 'BB', 'CC_DD']
['AA', 'BB', 'CC', 'DD']
如果有人找到更有效的解决方案,我很高兴在答案/cmets 中看到它!