最短常见超字符串问题的目标是找到包含给定集合中的每个字符串作为子字符串的最短可能字符串。我知道问题是“NP完成”。但是有针对该问题的近似策略。例如,给定短字符串
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.