简体   繁体   English

如何以一切可能的方式将字符串拆分为长度最多为3的连续子串?

[英]How to split a string into consecutive substrings of length at most 3 in all possible ways?

I am trying to take a string, between length 1 and 10, and output all possible ways of breaking up the string into consecutive substrings that are of sizes 1, 2, or 3. For example: 我试图在长度1和10之间取一个字符串,并输出所有可能的方法将字符串分解为大小为1,2或3的连续子字符串。例如:

Input: 123456

Slice the integer into individual characters, then proceed through to find combinations. 将整数切成单个字符,然后继续查找组合。 The code would return all of the following arrays. 代码将返回以下所有数组。

    [1, 2, 3, 4, 5, 6]  
    [12, 3, 4, 5, 6]  
    [1, 23, 4, 5, 6]  
    [1, 2, 34, 5, 6]  
    [1, 2, 3, 45, 6]  
    [1, 2, 3, 4, 56]  
    [12, 34, 5, 6]  
    [12, 3, 45, 6]  
    [12, 3, 4, 56]  
    [1, 23, 45, 6]  
    [1, 2, 34, 56]  
    [1, 23, 4, 56]  
    [12, 34, 56]  
    [123, 4, 5, 6]  
    [1, 234, 5, 6]  
    [1, 2, 345, 6]  
    [1, 2, 3, 456]  
    [123, 456]  
    [1, 23, 456]  
    [1, 234, 56]  
    [12, 345, 6]  
    [12, 3, 456]  
    [123, 4, 56]  
    [123, 45, 6]

I'm trying to do this in ruby. 我想用红宝石做这件事。 Thanks! 谢谢!

Here's a working function. 这是一个有效的功能。 May be not optimal as I didn't spend much time on it. 可能不是最佳的,因为我没有花太多时间。

str = "1234567890"

def f(s, n)
    return [[]] if s.empty?

    (1..[n, s.length].min).map{|c| f(s[c..-1], n).map{|a| [s[0, c]] + a}}.inject(&:+)
end

puts f(str, 3).collect{|l| l * "\t"}

EDIT: Made it a bit shorter and the length is now passed as second parameter to function for flexibility. 编辑:使它缩短一点,现在作为第二个参数传递长度以实现灵活性。

It took me quite a while to figure this out, its a much harder problem then I first though. 我花了很长时间来弄明白这一点,这是我第一次遇到的更难的问题。 But eventually I hit upon this solution: 但最终我遇到了这个解决方案:

def combinations(s)
  c = (s.length > 3) ? [] : [[s]]
  max = [4, s.length].min
  (1...max).inject(c) do |c, i|
    combinations(s[i..-1]).inject(c) do |c, tail|
      c.push([s[0...i]] + tail)
    end
  end
end

combinations("12345").each { |c| p c }

Produces: 生产:

["1", "2", "345"]
["1", "2", "3", "45"]
["1", "2", "3", "4", "5"]
["1", "2", "34", "5"]
["1", "23", "45"]
["1", "23", "4", "5"]
["1", "234", "5"]
["12", "345"]
["12", "3", "45"]
["12", "3", "4", "5"]
["12", "34", "5"]
["123", "45"]
["123", "4", "5"]

Here's another: 这是另一个:

class String
  def splitup(prefix=nil)
    parts = []
    if size <= 3
      parts << [prefix,self].compact * ","
    end
    (1..([size,3].min)).each do |p|
      next if p >= size
      parts << slice(p..-1).splitup([prefix,slice(0,p)].compact * ",")
    end
    parts
  end

  def report
    flat = splitup.flatten.sort_by {|x| [-x.size,x]}
    puts
    puts "#{flat.size} permutations of #{self}"
    puts flat
  end
end

and then 然后

>> "123456".report

24 permutations of 123456
1,2,3,4,5,6
1,2,3,4,56
1,2,3,45,6
1,2,34,5,6
1,23,4,5,6
12,3,4,5,6
1,2,3,456
1,2,34,56
1,2,345,6
1,23,4,56
1,23,45,6
1,234,5,6
12,3,4,56
12,3,45,6
12,34,5,6
123,4,5,6
1,23,456
1,234,56
12,3,456
12,34,56
12,345,6
123,4,56
123,45,6
123,456
def f(s, mx)
  (1..[s.size, mx].min).each_with_object([]) do |n,a|
     if n < s.size
       sf = s[0, n]
       f(s[n..-1], mx).each { |c| a << [sf, *c] }
     else
       a << [s]
     end
  end
end

f("12345", 3)
  #=> [["1", "2", "3", "4", "5"], ["1", "2", "3", "45"],
  #    ["1", "2", "34", "5"], ["1", "2", "345"], ["1", "23", "4", "5"],
  #    ["1", "23", "45"], ["1", "234", "5"], ["12", "3", "4", "5"],
  #    ["12", "3", "45"], ["12", "34", "5"], ["12", "345"],
  #    ["123", "4", "5"], ["123", "45"]] => [["1", "2", "3", "4"],
  #    ["1", "2", "34"], ["1", "23", "4"], ["1", "234"],
  #    ["12", "3", "4"], ["12", "34"], ["123", "4"]] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM