关于最长公共子序列的知识和概念请大家参考这篇博文,因为时间问题,这里只写了代码实现。使用python3.5
链接地址:点击打开链接
import numpy as np
x=input()
y=sorted(x)
n=len(x)
c=np.zeros((n+1,n+1),dtype=int)
b=np.zeros((n+1,n+1),dtype=int)
def LIS(x,y):
for i in range(1,n+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,j]=1
elif c[i-1,j]>c[i,j-1]:
c[i,j]=c[i-1,j]
b[i,j]=3
else:
c[i,j]=c[i,j-1]
b[i,j]=2
return b,c
def PRINT_LIS(b,x,i,j):
if i==0 or j==0:
return
if b[i,j]==1:
PRINT_LIS(b,x,i-1,j-1)
print(x[i-1])
elif b[i,j]==2:
PRINT_LIS(b,x,i,j-1)
else:
PRINT_LIS(b,x,i-1,j)
LIS(x,y)
PRINT_LIS(b,x,n,n)