"the" -> 10, "quick" -> 5, "brown" -> 3, "fox" -> 8 对于上面的单个单词,我将取一个数组 [10,5,3,8] 来表示单词0,1,2,3 遍历列表,如果两个分数的组合小于组合分数,例如 10+5 >5 the + quick >the quick 5+3 < 10 quick brown > quick + brown 。标记这个等等
在标记组合解决方案时,沿连续范围标记它们。例如,如果单词分数是 words = [1,2,5,3,1,4,6,2,6,8] 和 [4,6,9,7,8,2,9,1,2] 标记范围(包括两端)是 [0,1],[2,5],[6,7]
伪代码如下
从 0 遍历到 word length - 1
if number not in range :
add word[number] to overall sum.
else:
if length of range = 1 :
add combined_word_score [ lower_end_number]
else if length of range = 2 :
add combined_word_score [ lower_end_number+next number]
else if length of range > 2 and is odd number :
add max (alternate_score_starting at lower_end_number ,
word[lower_end]+word[higher_end]+alternate_score_starting at
next_number)
else if length of range > 2 and is even number :
add max (alternate_score_starting at lower_end_number +word[higher_end],
word[lower_end]+alternate_score_starting at
next_number).