字符串中子字符串的组合

发布于 2024-11-05 06:48:08 字数 261 浏览 3 评论 0原文

如何找到给定字符串中一组字符串的所有可能组合?

strings = ["ab","aa","ab","bb","ba","aba","aab"]
given_string = "abaababbab"

必须返回:

[
 ["ab","aa","ba","bb","ab],
 ["ab","aab","ab","ba","bab"]
 ["aba","aba","bb","ab"]
] 

How can I find all possible combinations of a set of strings in a given string?

strings = ["ab","aa","ab","bb","ba","aba","aab"]
given_string = "abaababbab"

has to return:

[
 ["ab","aa","ba","bb","ab],
 ["ab","aab","ab","ba","bab"]
 ["aba","aba","bb","ab"]
] 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-11-12 06:48:08

一个粗略的想法可能是这样的:

strings.select do |s|
  given_string.index(s)
end

这会给你:

["ab", "aa", "ab", "bb", "ba", "aba", "aab"]

我不确定你是否在这里寻找重复项。如果你不小心的话,你可能会遇到计算复杂的问题,因为这类事情使得 DNA 测序变得异常计算密集。

A rough idea might be something like this:

strings.select do |s|
  given_string.index(s)
end

This would give you:

["ab", "aa", "ab", "bb", "ba", "aba", "aab"]

I'm not sure if you're hunting for duplicates here or not. You may end up in a computationally complex problem if you're not careful as this sort of thing is what makes DNA sequencing extraordinarily compute-intensive.

盗心人 2024-11-12 06:48:08

以下:

strings = ["ab","aa","ab","bb","ba","aba","aab"]
@strings = strings.uniq!
@given_string = 'abaababbab'
@given_length = @given_string.length

def parse
  @parses.each do |parse|
    before = parse.pop # offset of the part that hasn't yet been parsed
    @strings.each do |str|
      next unless m = @given_string.match(str, before)
      first, last = m.offset(0)
      next unless first == before
      if last == @given_length
        @completed_parses.push([*parse, str])
      else
        @new_parses.push([*parse, str, last])
      end
    end
  end
  @parses = @new_parses
  @new_parses = []
  parse unless @parses.empty?
end

@parses = [[0]]
@new_parses = []
@completed_parses = []
parse
p @completed_parses

会给你:

[["aba", "aba", "bb", "ab"], ["ab", "aa", "ba", "bb", "ab"]]

我不明白为什么你的答案中有 ["ab","aab","ab","ba","bab"]

The following:

strings = ["ab","aa","ab","bb","ba","aba","aab"]
@strings = strings.uniq!
@given_string = 'abaababbab'
@given_length = @given_string.length

def parse
  @parses.each do |parse|
    before = parse.pop # offset of the part that hasn't yet been parsed
    @strings.each do |str|
      next unless m = @given_string.match(str, before)
      first, last = m.offset(0)
      next unless first == before
      if last == @given_length
        @completed_parses.push([*parse, str])
      else
        @new_parses.push([*parse, str, last])
      end
    end
  end
  @parses = @new_parses
  @new_parses = []
  parse unless @parses.empty?
end

@parses = [[0]]
@new_parses = []
@completed_parses = []
parse
p @completed_parses

will give you:

[["aba", "aba", "bb", "ab"], ["ab", "aa", "ba", "bb", "ab"]]

I don't get why you have ["ab","aab","ab","ba","bab"] in your answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文