輸入:
N, the number of chars in a string
e[0..N-1]: (b,c) an element of set e[a] means [a,b) is a substring with score c.
(如果所有子是可能的,那麼你可以只是有C(A,B)。)
通過如[1,2]我們是指覆蓋字符串第二個字母的子字符串(半開區間)。
(空子都是不允許的;如果他們成功了,那麼你可以妥善處理只有當你讓他們「考慮」最多k次)
輸出:
s[i] is the score of the best substring covering of [0,i)
a[i]: [a[i],i) is the last substring used to cover [0,i); else NULL
算法 - 如果區間e不稀疏,則爲O(N^2); O(N + E)其中e是允許間隔的總數。這有效地通過一個非循環圖尋找最佳路徑:
for i = 0 to N:
a[i] <- NULL
s[i] <- 0
a[0] <- 0
for i = 0 to N-1
if a[i] != NULL
for (b,c) in e[i]:
sib <- s[i]+c
if sib>s[b]:
a[b] <- i
s[b] <- sib
,得到最好的覆蓋三元組(A,B,C),其中的[A,B)爲c成本:
i <- N
if (a[i]==NULL):
error "no covering"
while (a[i]!=0):
from <- a[i]
yield (from,i,s[i]-s[from]
i <- from
當然,你可以在s [b]中存儲一對(sib,c)並保存減法。
您是否打算按照要求3在字符串中沒有空白(未被任何子字符串覆蓋的空白字符)? – 2009-09-11 07:23:55
這是正確的,不允許有間隙。 – 2009-09-11 07:26:27