python最长公共子序列或最长公共子串

本文介绍了如何使用动态规划解决最大公共子串和最大公共子序列问题,通过实例展示了如何通过子问题分解和动态公式构建来找到连续和非连续字符的最长共同部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态规划

  • 子问题分解,建立初始状态
  • 构建动态公式
import sys
#sys.stdout.write(str(123)+'\n')
import numpy as np

#最大公共子串-要求公共字符是连续的
def get_long_common_sub_string(str1, str2):
    # 初始化动态表
    len_str1 = len(str1)
    len_str2 = len(str2)

    dp_array = np.zeros((len_str1, len_str2),np.int8)
    # 初始化第一行和第一列
    value1 = str1[0]
    value2 = str2[0]

    for row in range(dp_array.shape[0]):
        if str1[row]==value2:
            dp_array[row][0]=1

    for col in range(dp_array.shape[1]):
        if str2[col]==value1:
            dp_array[0][col]=1

    max_length = 0
    max_index = 0
    for h in range(1, dp_array.shape[0]):
        for w in range(1, dp_array.shape[1]):
            if str1[h]==str2[w]:
                dp_array[h][w] = dp_array[h-1][w-1]+1;
                if max_length < dp_array[h][w]:
                    max_index = max(max_index, h)            
                max_length = max(max_length, dp_array[h][w])                

    return dp_array, str1[max_index - max_length+1:max_index+1]

#最大公共子序列,字符序列可以不连续,但顺序不可以乱,同样使用的是动态规划
def get_lcs(str1, str2):
    # 初始化动态表
    len_str1 = len(str1)
    len_str2 = len(str2)
    result=[]
    dp_array = np.zeros((len_str1, len_str2),np.int8)
    # 初始化第一行和第一列
    value1 = str1[0]
    value2 = str2[0]

    for row in range(dp_array.shape[0]):
        if str1[row]==value2:
            dp_array[row][0]=1
            result.append(value2)

    for col in range(dp_array.shape[1]):
        if str2[col]==value1:
            dp_array[0][col]=1
            if len(result)==0:
                result.append(value1)

    max_length = 0
    for h in range(1, dp_array.shape[0]):
        for w in range(1, dp_array.shape[1]):
            if str1[h]==str2[w]:
                dp_array[h][w] = dp_array[h-1][w-1] + 1
                if max_length < dp_array[h][w]:            
                    result.append(str1[h])
                max_length = max(max_length, dp_array[h][w])

            else:
                dp_array[h][w] = max(dp_array[h-1][w], dp_array[h][w-1])

    return dp_array, result

if __name__=="__main__":
    str1 = "abcdeffg"
    str2 = "abcfg"

    dp_array, sub_string = get_lcs(str1, str2)
    print(sub_string)
    for i in range(dp_array.shape[0]):
        sys.stdout.write('\n')
        for j in range(dp_array.shape[1]):
            sys.stdout.write(str(dp_array[i][j])+',')
            # print(dp_array[i][j]," ")

    # 通过递推公式,可以看出,res[i][j]的值来源于res[i-1][j]或者是res[i-1][j]和res[i][j-1]的较大值(可能相等)。
    # 我们将从最后一个元素c[8][9]倒推出S1和S2的LCS 
    #print(max_length)
    #print(dp_array[-1][-1])

    # value = dp_array[0][0]
    # index_list = []
    # for h in range(dp_array.shape[0]):
    #     for w in range(dp_array.shape[1]):
    #         if str1[h] == str2[w]:
    #             value = dp_array[h][w]
    #             index_list.append(w)
    #             sys.stdout.write(str(str2[w])+',')
    #             value = dp_array[h][w]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值