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

标签 python algorithm string

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

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

我正在寻找某种方法来对输入列表中的任意数量的字符串执行此操作。

谢谢,
焦达米利奥

最佳答案

仅举一个微不足道的例子还不够好。这几乎是(农历)年中最未明确说明的问题。然而,假设重叠只能发生在两端,并且每个单词只被测试两次(针对当前输出的每一端),并且您想要最大重叠,这将完成工作:

[修正错误后编辑]

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'

关于Python将字符串组合成最短的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697508/

相关文章:

c++ - 清理匹配元素的 for 循环

algorithm - 如何生成特定长度的随机字符串?

python - 如何检查 Python 脚本已经生成的字符串的 linux shell 编码

c++ - 查找具有最多不同字符的字符串

regex - Perl:如何仅替换字符串的匹配部分?

python - 导入本地函数的 PEP8 风格建议是什么?

python - 如何使用一个热编码 csv 文件作为 GroundTruth 将图像移动到文件夹中?

python - 如何在遵守列表顺序的同时将 pandas .replace() 与正则表达式列表一起使用?

Python Numpy 内存布局

algorithm - 检查数组的排序