Python:列表的最长公共子序列的长度 [英] Python: Length of longest common subsequence of lists

查看:205
本文介绍了Python:列表的最长公共子序列的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中是否有一个内置函数可以返回两个列表的最长公共子序列的长度?

Is there a built-in function in python which returns a length of longest common subsequence of two lists?

a=[1,2,6,5,4,8]
b=[2,1,6,5,4,4]

print a.llcs(b)

>>> 3

我试图找到最长的公共子序列,然后得到它的长度,但是我认为必须有一个更好的解决方案.

I tried to find longest common subsequence and then get length of it but I think there must be a better solution.

推荐答案

您可以轻松地将LCS重新构建为LLCS:

You can easily retool a LCS into a LLCS:

def lcs_length(a, b):
    table = [[0] * (len(b) + 1) for _ in xrange(len(a) + 1)]
    for i, ca in enumerate(a, 1):
        for j, cb in enumerate(b, 1):
            table[i][j] = (
                table[i - 1][j - 1] + 1 if ca == cb else
                max(table[i][j - 1], table[i - 1][j]))
    return table[-1][-1]

演示:

>>> a=[1,2,6,5,4,8]
>>> b=[2,1,6,5,4,4]
>>> lcs_length(a, b)
4

如果您需要最长的常见 substring (a 不同,但相关的问题(子序列是连续的),请使用:

If you wanted the longest common substring (a different, but related problem, where the subsequence is contiguous), use:

def lcsubstring_length(a, b):
    table = [[0] * (len(b) + 1) for _ in xrange(len(a) + 1)]
    l = 0
    for i, ca in enumerate(a, 1):
        for j, cb in enumerate(b, 1):
            if ca == cb:
                table[i][j] = table[i - 1][j - 1] + 1
                if table[i][j] > l:
                    l = table[i][j]
    return l

这与lcs_length动态编程方法非常相似,但是我们跟踪到目前为止找到的最大长度(因为不再保证表中的最后一个元素为最大).

This is very similar to the lcs_length dynamic programming approach, but we track the maximum length found so far (since it is no longer guaranteed the last element in the table is the maximum).

这将返回3:

>>> lcsubstring_length(a, b)
3

一个稀疏表变体,不必跟踪所有0:

A sparse table variant to not have to track all the 0s:

def lcsubstring_length(a, b):
    table = {}
    l = 0
    for i, ca in enumerate(a, 1):
        for j, cb in enumerate(b, 1):
            if ca == cb:
                table[i, j] = table.get((i - 1, j - 1), 0) + 1
                if table[i, j] > l:
                    l = table[i, j]
    return l

这篇关于Python:列表的最长公共子序列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆