English 中文(简体)
三个字符串的最长期共同的子序列
原标题:Longest Common Subsequence of three strings
  • 时间:2012-05-24 22:39:46
  •  标签:
  • python

我写了这些函数(它有效),以找到两个字符串中最长的共同后继序列。

def lcs_grid(xs, ys):
    grid = defaultdict(lambda: defaultdict(lambda: (0,"")))
    for i,x in enumerate(xs):
        for j,y in enumerate(ys):
            if x == y:
                grid[i][j] = (grid[i-1][j-1][0]+1, \ )
            else:
                if grid[i-1][j][0] > grid[i][j-1][0]:
                    grid[i][j] = (grid[i-1][j][0], < )
                else:
                    grid[i][j] = (grid[i][j-1][0], ^ )

    return grid

def lcs(xs,ys):
    grid = lcs_grid(xs,ys)
    i, j = len(xs) - 1, len(ys) - 1

    best = []
    length,move = grid[i][j]
    while length:
        if move ==  \ :
            best.append(xs[i])
            i -= 1
            j -= 1
        elif move ==  ^ :
            j -= 1
        elif move ==  < :
            i -= 1
        length,move = grid[i][j]

    best.reverse()
    return best

是否有人提议修改函数 st. t., 他们可以打印三个字符串中最长的常见子序列? I. e. 函数调用为 : < code> lcs (str1, str2, str3)

直到现在,我用减值声明来管理它, 但我想有一个功能, 真正打印出后继序列 而不减值 - 声明。

问题回答

要找到 D字符串中最长的共同子字符串, 您不能简单地使用 < code> duce , 因为3字符串中最长的共同子字符串不必是两个字符串中任何一个的 LCS 的子字符串。 反示例 :

a = "aaabb"
b = "aaajbb"
c = "cccbb"

例如,LCS(a,b)=“aaaa”和LCS(a,b,c)=“bb”。如你所见,“bb”不是“aaa”的子字符串。

在你的情况下,自从你实施了动态编程版本后, 你必须建立一个D维网格, 并相应调整算法 。

您可能想要查看后缀树, 这应该能让事情更快一些, 见< a href="http://en.wikipedia.org/wiki/Longest_common_substring_popleblem" rel=“ 不跟随 nofolrererer" >Wikipedia 。 还要查看此 < a href=> https://stackoverflow.com/ questions/28921/ longest-com-substring- from- more- than- two- strings- python" 问题 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签