Python将字符串组合成最短的字符串?

时间:2011-01-15 01:23:10

标签: python algorithm string

如果我有一个字符串列表,我想将它们组合成一个重叠字符的字符串。如果没有重叠的字符串,只需将其添加到结尾。这是一个过于简化的版本。

input = ['one', 'two']
output = 'twone'

我在寻找输入列表中任意数量的字符串的一些方法。

谢谢,
giodamelio

2 个答案:

答案 0 :(得分:5)

给出一个微不足道的例子真的不够好。这是关于(农历)年最不明确的问题。然而,假设重叠只能在结束时发生,并且每个单词仅测试两次(对于当前输出的每一端),并且您想要最大重叠,这将完成工作:

[修正后修正错误]

def glue(a, b):
    maxn = 0
    for n in xrange(1, 1 + min(len(a), len(b))):
        suffix = a[-n:]
        prefix = b[:n]
        if prefix == suffix:
            maxn = n
    # BUG: return maxn, a[:-maxn] + b
    # FAILS when maxn == 0
    # EXTRA TEST: ['nil', 'overlap']
    return a + b[maxn:]     


def multiglue(words):
    if not words: return ""
    result = words[0]
    for word in words[1:]:
        nx, rx = glue(result, word)
        ny, ry = glue(word, result)
        result = rx if nx > ny else ry
    return result

tests = [line.split() for line in """
    one
    two one
    one two
    overlap nil
    nil overlap
    toad dog rabbit
    frog ogham
    ogham frog
    hopper grasshopper
    grass grasshopper person
    foooo oooof
    oooof foooo""".splitlines()]

for test in tests:
    out = multiglue(test)
    print test, repr(out)

输出:

[] ''
['one'] 'one'
['two', 'one'] 'twone'
['one', 'two'] 'twone'
['overlap', 'nil'] 'niloverlap'
['nil', 'overlap'] 'overlapnil'
['toad', 'dog', 'rabbit'] 'rabbitoadog'
['frog', 'ogham'] 'frogham'
['ogham', 'frog'] 'frogham'
['hopper', 'grasshopper'] 'grasshopper'
['grass', 'grasshopper', 'person'] 'grasshopperson'
['foooo', 'oooof'] 'foooof'
['oooof', 'foooo'] 'foooof'

答案 1 :(得分:2)

您应该可以使用Needleman-Wunsch algorithm的修改版本。从你的问题我不清楚组合字符串必须满足什么约束(一个字符串必须整体嵌入另一个字符串,或者一个字符串可以从另一个字符串混合?),所以我不会修改它你,但这应该足以让你至少开始。

请参阅http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm

此外,http://www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf