我必须找到给定单词的所有组合,其长度加起来等于10。输入将如下所示:
words = [
["act", "bat", "cat"],
["acta"],
[],
["mounts"],
["amounts", "contour"],
["boo", "con", "coo"],
["tots", "tour"],
["mumus"],
["catamounts"]
]
我希望这样的输出
[
["act", "boo", "tots"],
["act", "boo", "tour"],
["act", "con", "tots"],
["act", "con", "tour"],
["act", "coo", "tots"],
["act", "coo", "tour"],
["bat", "boo", "tots"],
["bat", "boo", "tour"],
["bat", "con", "tots"],
["bat", "con", "tour"],
["bat", "coo", "tots"],
["bat", "coo", "tour"],
["cat", "boo", "tots"],
["cat", "boo", "tour"],
["cat", "con", "tots"],
["cat", "con", "tour"],
["cat", "coo", "tots"],
["cat", "coo", "tour"],
["act", "amounts"],
["act", "contour"],
["bat", "amounts"],
["bat", "contour"],
["cat", "amounts"],
["cat", "contour"],
["acta", "mounts"],
["catamounts"]
]
答案 0 :(得分:0)
在我看来,就所需的计算数量而言,这是一种接近最佳的方法。我将首先回答特定问题,然后提出一种递归方法,该方法将为一般问题生成所需的组合。
解决特定问题
我们看到有两个由三个字母组成的单词数组。我会写2-3。同样,也有2-4、1-5、1-6、1-7和1-10。我们可以忘记空数组。
元素如何求和为10?可能性如下:3-3-4、3-7、4-6、1-10。
我们只需要为每个组合计算组合并采用它们的并集。
对于3-3-4,我们必须从以下每个数组中选取一个元素:
["act", "bat", "cat"]
["boo", "con", "coo"]
["acta"] | ["tots", "tour"]
#=> ["acta", "tots", "tour"]
我们可以使用Array#product来计算这些组合:
["act", "bat", "cat"].product(["boo", "con", "coo"],
["acta", "tots", "tour"])
#=> [["act", "boo", "acta"], ["act", "boo", "tots"],
# ["act", "boo", "tour"], ["act", "con", "acta"],
# ["act", "con", "tots"], ["act", "con", "tour"],
# ["act", "coo", "acta"], ["act", "coo", "tots"],
# ["act", "coo", "tour"], ["bat", "boo", "acta"],
# ["bat", "boo", "tots"], ["bat", "boo", "tour"],
# ["bat", "con", "acta"], ["bat", "con", "tots"],
# ["bat", "con", "tour"], ["bat", "coo", "acta"],
# ["bat", "coo", "tots"], ["bat", "coo", "tour"],
# ["cat", "boo", "acta"], ["cat", "boo", "tots"],
# ["cat", "boo", "tour"], ["cat", "con", "acta"],
# ["cat", "con", "tots"], ["cat", "con", "tour"],
# ["cat", "coo", "acta"], ["cat", "coo", "tots"],
# ["cat", "coo", "tour"]]
类似地计算3-7、4-6和1-10的组合。
广义问题的递归方法
def doit(words, target)
recurse(words.reject do |a|
a.empty? || a.first.size > target
end.sort_by { |a| a.first.size }, target)
end
def recurse(sorted, target)
arr, *rest = sorted
sz = arr.first.size
if rest.empty?
return sz == target ? arr.map { |s| [s] } : []
end
return [] if sz > target
b = recurse(rest, target) # include no element of arr
return b if sz != target && sz > target/2
case target <=> sz
when -1
b
when 0
d = arr.map { |s| [s] }
b.empty? ? d : b + d
else # 1
c = recurse(rest, target-sz)
if c.empty?
b
else
d = arr.product(c).map { |s,c| [s] + c }
b.empty? ? d : b + d
end
end
end
如果我们执行
doit words, 10
返回上一节中显示的解决方案,尽管元素的顺序不同。
说明
说明递归的工作方式具有挑战性。我认为最好的方法是插入puts
语句。但是,仅凭这还不够,因为它很快就使该方法的哪个实例被调用感到困惑。因此,我建议缩进结果,就像我在下面所做的那样。
INDENT = 4
def indent
@offset += INDENT
puts
end
def undent
@offset -= INDENT
puts
end
def pu(str)
puts ' '*@offset + str
end
def doit(words, target)
@offset = -INDENT #***
recurse(words.reject do |a|
a.empty? || a.first.size > target
end.sort_by { |a| a.first.size }, target)
end
def recurse(sorted, target)
indent #***
puts #***
pu "sorted=#{sorted}" #***
pu "target=#{target}" #***
arr, *rest = sorted
sz = arr.first.size
pu "arr=#{arr}, rest=#{rest}, sz=#{sz}" #***
if rest.empty?
pu "returning (sz==target ? arr.map { |s| [s] } : [])="
pu "#{(sz==target ? arr.map { |s| [s] } : [])}" #***
undent #***
return sz == target ? arr.map { |s| [s] } : []
end
if sz > target
pu "returning [] as sz > target=#{sz > target}" #***
undent #***
return []
end
pu "calling recurse(#{rest}, #{target}) w/o using an element of #{arr}" #***
b = recurse(rest, target) # include no element of arr
pu "b=#{b}" #***
pu "target=#{target}, sz=#{sz}, target-sz=#{target-sz}" #***
if sz != target && sz > target/2
pu "returning b as sz != target && sz > target/2" #***
undent #***
return b
end
case target <=> sz
when -1
pu " target < sz" #***
b
when 0
pu " target == sz" #***
d = arr.map { |s| [s] }
b.empty? ? d : b + d
else # 1
pu " target > sz" #***
pu "calling recurse(#{rest}, #{target-sz}) using an element of #{arr}" #***
c = recurse(rest, target-sz)
pu "c=#{c}" #***
if c.empty?
b
else
d = arr.product(c).map { |s,c| [s] + c }
b.empty? ? d : b + d
end
end.
tap do |a|
pu "returning a=#{a}" #***
undent #***
end
end
这是一个更简单的示例。我将仅显示输出示例。感兴趣的读者可能希望运行代码并研究结果。
test_words = [["ac", "ba"], ["bo"],
["acta"], ["tots", "tour"]]
doit test_words, 8
显示以下内容。
sorted=[["ac", "ba"], ["bo"], ["acta"], ["tots", "tour"]]
target=8
arr=["ac", "ba"], rest=[["bo"], ["acta"], ["tots", "tour"]], sz=2
calling recurse([["bo"], ["acta"], ["tots", "tour"]], 8) w/o using an element of ["ac", "ba"]
sorted=[["bo"], ["acta"], ["tots", "tour"]]
target=8
arr=["bo"], rest=[["acta"], ["tots", "tour"]], sz=2
calling recurse([["acta"], ["tots", "tour"]], 8) w/o using an element of ["bo"]
...
b=[["acta", "tots"], ["acta", "tour"]]
target=8, sz=2, target-sz=6
target > sz
calling recurse([["acta"], ["tots", "tour"]], 6) using an element of ["bo"]
sorted=[["acta"], ["tots", "tour"]]
target=6
arr=["acta"], rest=[["tots", "tour"]], sz=4
calling recurse([["tots", "tour"]], 6) w/o using an element of ["acta"]
...
c=[["tots"], ["tour"], ["acta"]]
returning a=[["bo", "tots"], ["bo", "tour"], ["bo", "acta"]]
c=[["bo", "tots"], ["bo", "tour"], ["bo", "acta"]]
returning a=[["acta", "tots"], ["acta", "tour"], ["ac", "bo", "tots"], ["ac", "bo", "tour"], ["ac", "bo", "acta"], ["ba", "bo", "tots"], ["ba", "bo", "tour"], ["ba", "bo", "acta"]]
#=> [["acta", "tots"], ["acta", "tour"],
# ["ac", "bo", "tots"], ["ac", "bo", "tour"],
# ["ac", "bo", "acta"], ["ba", "bo", "tots"],
# ["ba", "bo", "tour"], ["ba", "bo", "acta"]]