我正在尋找一種算法來解決以下問題:序列覆蓋算法
鑑於從0
序列n
包含數字到9
和m
其他序列,找出最小的(含最低金額)系列的序列等於n
。
例
n = 123456
m1 = 12
m2 = 34
m3 = 56
m4 = 3456
output = m1 + m4 = 123456
事情我已經想到了到目前爲止
基本貪婪技術採用FSM或者特里樹樹查找最長序列中開始安裝:
while n not null
longest = the longest sequence fitting in the beginning of n
print longest
n = n - longest
反例
n = 123456
m1 = 12
m2 = 1
m3 = 23456
m4 = 3
m5 = 4
m6 = 5
m7 = 6
algorithm will find m1 + m4 + m5 + m6 + m7 (12 + 3 + 4 + 5 + 6)
algorithm should find m2 + m3 (1 + 23456)
另一個貪心法
array = {n} #this represents words to be matched
while array not empty
(word, index) = find longest sequence covering part of any word in array and its index
split array[index] into two words - first before found word, second after it
if any of those split items is null
remove it
反例
n = 12345678
m1 = 3456
m2 = 1
m3 = 2
m4 = 7
m5 = 8
m6 = 123
m7 = 45
m8 = 678
algorithm will find m2 + m3 + m1 + m4 + m5 (1 + 2 + 3456 + 7 + 8)
algorithm should find m6 + m7 + m8 (123 + 45 + 678)
我不明白你的問題的聲明。 –