自用:最长公共子序列LCS

自用:最长公共子序列LCS
蛮力算法如下

def sub(list): #求序列的每个子序列
    l = []
    size = len(list)
    count = 2 ** size
    for i in range(count):
        arr = []
        for j in range(size):
            if (i >> j) % 2:
                arr.append(list[j])
        l.append(arr)
    return l


def compareXY(XX,Y):#用每个X的子序列去与Y对比,输出LCS,返回序列长度
    size=0
    LCS=None
    if len(XX)==0:
        return -1
    else:
        for index in XX:
            if compare(index,Y):
                if len(index)>size:
                    size=len(index)
                    LCS=index
        print(LCS)
        return size


def compare(x,y):#x是否为y的子序列
    i,j=0,0
    while i<len(x) and j<len(y):
        if x[i]==y[j]:
            i+=1
            j+=1
        else:
            j+=1
    if i==len(x):
        return True
    else:
        return False


if __name__ == '__main__':
    X=["A","B","C","B","D","A","B"]
    Y=["B","D","C","A","B","A"]
    XX=sub(X)
    #输出X的所有子序列
    print(XX)
    #输出LCS,返回序列长度
    print(compareXY(XX,Y))

'''
[[], ['A'], ['B'], ['A', 'B'], ['C'], ['A', 'C'], ['B', 'C'], ['A', 'B', 'C'], ['B'], ['A', 'B'], ['B', 'B'], ['A', 'B', 'B'], ['C', 'B'], ['A', 'C', 'B'], ['B', 'C', 'B'], ['A', 'B', 'C', 'B'], ['D'], ['A', 'D'], ['B', 'D'], ['A', 'B', 'D'], ['C', 'D'], ['A', 'C', 'D'], ['B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['B', 'D'], ['A', 'B', 'D'], ['B', 'B', 'D'], ['A', 'B', 'B', 'D'], ['C', 'B', 'D'], ['A', 'C', 'B', 'D'], ['B', 'C', 'B', 'D'], ['A', 'B', 'C', 'B', 'D'], ['A'], ['A', 'A'], ['B', 'A'], ['A', 'B', 'A'], ['C', 'A'], ['A', 'C', 'A'], ['B', 'C', 'A'], ['A', 'B', 'C', 'A'], ['B', 'A'], ['A', 'B', 'A'], ['B', 'B', 'A'], ['A', 'B', 'B', 'A'], ['C', 'B', 'A'], ['A', 'C', 'B', 'A'], ['B', 'C', 'B', 'A'], ['A', 'B', 'C', 'B', 'A'], ['D', 'A'], ['A', 'D', 'A'], ['B', 'D', 'A'], ['A', 'B', 'D', 'A'], ['C', 'D', 'A'], ['A', 'C', 'D', 'A'], ['B', 'C', 'D', 'A'], ['A', 'B', 'C', 'D', 'A'], ['B', 'D', 'A'], ['A', 'B', 'D', 'A'], ['B', 'B', 'D', 'A'], ['A', 'B', 'B', 'D', 'A'], ['C', 'B', 'D', 'A'], ['A', 'C', 'B', 'D', 'A'], ['B', 'C', 'B', 'D', 'A'], ['A', 'B', 'C', 'B', 'D', 'A'], ['B'], ['A', 'B'], ['B', 'B'], ['A', 'B', 'B'], ['C', 'B'], ['A', 'C', 'B'], ['B', 'C', 'B'], ['A', 'B', 'C', 'B'], ['B', 'B'], ['A', 'B', 'B'], ['B', 'B', 'B'], ['A', 'B', 'B', 'B'], ['C', 'B', 'B'], ['A', 'C', 'B', 'B'], ['B', 'C', 'B', 'B'], ['A', 'B', 'C', 'B', 'B'], ['D', 'B'], ['A', 'D', 'B'], ['B', 'D', 'B'], ['A', 'B', 'D', 'B'], ['C', 'D', 'B'], ['A', 'C', 'D', 'B'], ['B', 'C', 'D', 'B'], ['A', 'B', 'C', 'D', 'B'], ['B', 'D', 'B'], ['A', 'B', 'D', 'B'], ['B', 'B', 'D', 'B'], ['A', 'B', 'B', 'D', 'B'], ['C', 'B', 'D', 'B'], ['A', 'C', 'B', 'D', 'B'], ['B', 'C', 'B', 'D', 'B'], ['A', 'B', 'C', 'B', 'D', 'B'], ['A', 'B'], ['A', 'A', 'B'], ['B', 'A', 'B'], ['A', 'B', 'A', 'B'], ['C', 'A', 'B'], ['A', 'C', 'A', 'B'], ['B', 'C', 'A', 'B'], ['A', 'B', 'C', 'A', 'B'], ['B', 'A', 'B'], ['A', 'B', 'A', 'B'], ['B', 'B', 'A', 'B'], ['A', 'B', 'B', 'A', 'B'], ['C', 'B', 'A', 'B'], ['A', 'C', 'B', 'A', 'B'], ['B', 'C', 'B', 'A', 'B'], ['A', 'B', 'C', 'B', 'A', 'B'], ['D', 'A', 'B'], ['A', 'D', 'A', 'B'], ['B', 'D', 'A', 'B'], ['A', 'B', 'D', 'A', 'B'], ['C', 'D', 'A', 'B'], ['A', 'C', 'D', 'A', 'B'], ['B', 'C', 'D', 'A', 'B'], ['A', 'B', 'C', 'D', 'A', 'B'], ['B', 'D', 'A', 'B'], ['A', 'B', 'D', 'A', 'B'], ['B', 'B', 'D', 'A', 'B'], ['A', 'B', 'B', 'D', 'A', 'B'], ['C', 'B', 'D', 'A', 'B'], ['A', 'C', 'B', 'D', 'A', 'B'], ['B', 'C', 'B', 'D', 'A', 'B'], ['A', 'B', 'C', 'B', 'D', 'A', 'B']]
['B', 'C', 'B', 'A']
4
'''
  • (sub函数)找出序列的所有子序列
  • (compare函数)是否为另一序列的子序列:关键在于把短序列中的每个元素都与长序列比较,相等就各自数组指针+1,最后看长度是否与短序列相等

自用:最长公共子序列LCS

import numpy as np


def LCS(X,Y):
    m=len(X)
    n=len(Y)
    c=np.zeros((m+1,n+1),dtype=np.int)
    b=np.zeros((m,n))
    for i in range(1,m+1):
        for j in range(1,n+1):
            if X[i-1]==Y[j-1]:
                c[i][j]=c[i-1][j-1]+1
                b[i-1][j-1]=1  #↖
            else:
                c[i][j]=c[i-1][j] if c[i-1][j]>c[i][j-1] else c[i][j-1]
                b[i-1][j-1] =2 if c[i-1][j]>c[i][j-1] else 3 #↑,←
    print(c)
    sequence(b, X, m - 1, n - 1)


def sequence(b,x,i,j):
    if i==-1 or j==-1:
        return
    if b[i][j]==1:#↖
        sequence(b,x,i-1,j-1)
        print(x[i],end=" ")
    elif b[i][j]==2:#↑
        sequence(b,x,i-1,j)
    else:#←
        sequence(b,x,i,j-1)


if __name__ == '__main__':
    X=["A","B","C","B","D","A","B"]
    Y=["B","D","C","A","B","A"]
    LCS(X,Y)
上一篇:06-图3 六度空间


下一篇:PTA —— Is Topological Order