簡體   English   中英

如何以一切可能的方式將字符串拆分為長度最多為3的連續子串?

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

我試圖在長度1和10之間取一個字符串,並輸出所有可能的方法將字符串分解為大小為1,2或3的連續子字符串。例如:

Input: 123456

將整數切成單個字符,然后繼續查找組合。 代碼將返回以下所有數組。

    [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]

我想用紅寶石做這件事。 謝謝!

這是一個有效的功能。 可能不是最佳的,因為我沒有花太多時間。

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"}

編輯:使它縮短一點,現在作為第二個參數傳遞長度以實現靈活性。

我花了很長時間來弄明白這一點,這是我第一次遇到的更難的問題。 但最終我遇到了這個解決方案:

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 }

生產:

["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"]

這是另一個:

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

然后

>> "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