作爲開始,你至少可以通過計算set symmetric difference減少的問題,以消除不發生兩三胞胎任何序列。
對於最長的子序列,算法使用dynamic programming方法。對於每個三元組,找到兩者中出現的最短的長度爲2的子串。循環這些對,試圖通過組合對來擴展它們。繼續擴展,直到您擁有每個三元組的最長擴展。挑選最長的那些:
ABQACBBA
ZBABBA
Eliminate symmetric difference
ABABBA and BABBA
Start with the first A in ABABBA.
It is followed by B, giving the elements [0,1]
Check to see if AB is in BABBA, and record a match at [1,2]
So, the first pair is ((0,1), (1,2))
Next, try the first B in ABABBA.
It is followed by an A giving the elements [1,2]
Check to see if BA is in BABBA and record a match at [0,1]
Continue with the rest of the letters in ABABBA.
Then, try extensions.
The first pair AB at [0,1] and [1,2] can be extended from BA
to ABA [0,1,3] and [1,2,4]. Note, the latter group is all the
way to the right so it cannot be extended farther. ABA cannot
be extended.
Continue until all sequences have extended are far as possible.
Keep only the best of those.
我認爲這是這裏的錯誤算法。目標不是像提取最長的公共子序列那樣對齊序列。序列比對算法運行效率低於LCS算法,我認爲序列比對算法產生的解決方案可能無法很好地映射到OP想要的內容。 – templatetypedef 2011-12-19 19:23:31
@templatetypedef:我同意。對齊是對特定問題比需要的更一般的方法。這就是爲什麼我upvoted你的解決方案:)。儘管我需要更詳細的方法,但我離開了它。 – Avaris 2011-12-19 20:50:31