Ruby 红宝石最短公共超弦
最短公共超弦问题的目标是寻找最短超弦 可能的字符串,它包含给定集合中作为子字符串的每个字符串。我知道这个问题是NP完全问题。但是对于这个问题有一些近似策略。 例如给定短字符串Ruby 红宝石最短公共超弦,ruby,algorithm,Ruby,Algorithm,最短公共超弦问题的目标是寻找最短超弦 可能的字符串,它包含给定集合中作为子字符串的每个字符串。我知道这个问题是NP完全问题。但是对于这个问题有一些近似策略。 例如给定短字符串 ABRAC ACADA ADABR DABRA RACAD 如何实现最短公共超弦问题,从而使上述给定字符串的输出为ABRACADABRA? 另一个例子 Given fegiach bfgiak hfdegi iakhfd fgiakhg 弦 bfgiakhfdegiach是长度为15的可能解决方案 我想在Ruby中实
ABRAC
ACADA
ADABR
DABRA
RACAD
如何实现最短公共超弦问题,从而使上述给定字符串的输出为ABRACADABRA
?
另一个例子
Given
fegiach
bfgiak
hfdegi
iakhfd
fgiakhg
弦
bfgiakhfdegiach
是长度为15的可能解决方案
我想在Ruby中实现这一点,虽然我没有深入研究算法,但我正在努力改进算法
天真贪婪的实现将涉及为每个子字符串创建一个后缀数组
def suffix_tree(string)
size = string.length
suffixes = Array.new(size)
size.times do |i|
suffixes[i] = string.slice(i, size)
end
suffixes
end
#store the suffixes in a hash
#key is a fragment, value = suffixes
def hash_of_suffixes(fragments)
suffixes_hash = Hash.new
fragments.each do |frag|
suffixes_hash["#{frag}"]= suffix_tree(frag)
end
suffixes_hash
end
fragments = ['ABRAC','ACADA','ADABR','DABRA','RACAD']
h = hash_of_suffixes(fragments)
#then search each fragment in all the suffix trees and return the number of
#overlaps for each key
#store the results in graph??
#find possible ordering of the fragments
I would be grateful with some help.
请注意您的示例中指出问题的注释。还要注意的是,如果有一些巧妙的方法来做到这一点,我不知道它是什么。我只是迭代所有的排列,把它们放在一起,然后找到最短的
class ShortestSuperstring
def initialize(*strings)
self.strings = strings
end
def call
@result ||= smoosh_many strings.permutation.min_by { |permutation| smoosh_many(permutation.dup).size }
end
private
attr_accessor :strings
def smoosh_many(permutation, current_word='')
return current_word if permutation.empty?
next_word = permutation.shift
smoosh_many permutation, smoosh_two(current_word, next_word)
end
def smoosh_two(base, addition)
return base if base.include? addition
max_offset(base, addition).downto 0 do |offset|
return base << addition[offset..-1] if addition.start_with? base[-offset, offset]
end
end
def max_offset(string1, string2)
min string1.size, string2.size
end
def min(n1, n2)
n1 < n2 ? n1 : n2
end
end
也许是遗传算法?你的两个例子都是错误的,
['ABRAC','ACADA','ADABR','DABRA','RACAD']
的最短超弦要么是“RACADABRAC”
要么是“ADABRACADA”
。和“fegiach”
,和“fgiakhg”
甚至不在字符串“bfgiakhfdegiach”
中。谢谢。我可能做得很快,但你明白我的意思。。包含所有子字符串的最短字符串。感谢分享该方法!
describe ShortestSuperstring do
def self.ss(*strings, possible_results)
example "#{strings.inspect} can come from superstrings #{possible_results.inspect}" do
result = described_class.new(*strings).call
strings.each { |string| result.should include string }
possible_results.should include(result), "#{result.inspect} was not an expected superstring."
end
end
ss '', ['']
ss "a", "a", "a", ['a']
ss "a", "b", %w[ab ba]
ss 'ab', 'bc', ['abc']
ss 'bc', 'ab', ['abc']
ss 'abcd', 'ab', ['abcd']
ss 'abcd', 'bc', ['abcd']
ss 'abcd', 'cd', ['abcd']
ss 'abcd', 'a', 'b', 'c', 'd', ['abcd']
ss 'abcd', 'a', 'b', 'c', 'd', 'ab', 'cd', 'bcd', ['abcd']
%w[ABRAC ACADA ADABR DABRA RACAD].permutation.each do |permutation|
ss *permutation, %w[RACADABRAC ADABRACADA]
end
%w[fegiach bfgiak hfdegi iakhfd fgiakhg].permutation.each do |permutation|
ss *permutation, %w[bfgiakhgiakhfdegifegiach
bfgiakhgfegiachiakhfdegi
iakhfdegibfgiakhgfegiach
iakhfdegibfgiakhgfegiach
fegiachiakhfdegibfgiakhg
fegiachbfgiakhgiakhfdegi
iakhfdegifegiachbfgiakhg]
end
end