1. 程式人生 > >python實現Longest Common Subsequence最長公共子序列演算法

python實現Longest Common Subsequence最長公共子序列演算法

    最長公共子序列是很基本的演算法,只是最近用到了就又拿來學習一下,網上有很多很多的Java版本的,的確寫的也很不錯,思想都很好,大致上分為三種:

1.基於遞迴的思想

2.基於動態規劃的思想

3.基於HashMap的動態規劃

    在這裡我使用的是python來實現,方法很簡單,先看程式:

#!usr/bin/env python
#encoding:utf-8

'''
__Author__:沂水寒城
功能:計算字串之間的最長公共子序列
'''

def get_lcs(string1, string2):
    '''
    輸入:待比較的兩個字串
    輸出:降序輸出的(子序列長度,子序列)列表
    '''
    string1_list=list(string1)
    string2_list=list(string2)
    lcs_list=[]
    for i in range(len(string1_list)):
        flag=0
        lcs=''
        for j in range(i,len(string1_list)):
            for k in range(flag, len(string2_list)):
                if string1_list[j]==string2_list[k]:
                    lcs+=string1_list[j]
                    flag=k+1
        lcs_list.append((len(lcs), lcs))
    print len(lcs_list)
    return sorted(lcs_list, reverse=True)


if __name__ == '__main__':
    lcs_list=get_lcs("abcdjio7890bhsdjknyewhbnvd", "djio78347bvfdjbnknyew")
    print lcs_list

結果如下:
26
[(11, 'io77bbknyew'), (10, 'o77bbknyew'), (9, 'ddjbknyew'), (9, 'ddjbknyew'), (9, '77bbknyew'), (8, 'jjbknyew'), (8, 'ddjknyew'), (8, 'ddjknyew'), (8, 'ddjknyew'), (8, '8bbknyew'), (7, 'jjknyew'), (7, 'bbknyew'), (7, 'bbknyew'), (7, 'bbknyew'), (7, 'bbknyew'), (7, 'bbknyew'), (5, 'nnyew'), (5, 'knyew'), (4, 'bbnn'), (4, 'bbnn'), (3, 'yew'), (2, 'vd'), (2, 'nn'), (2, 'ew'), (2, 'dd'), (1, 'w')]
[Finished in 0.5s]

    演算法的思想也很簡單,這裡簡單說一下:首先將字串轉換成字串列表形式,以第一個字串為基準開始迴圈遍歷比較,設定標誌位,目的是為了只比較第二個字串跟第一個字串字元相同位置之後的字串,而不重頭開始比較,保證了效率的同時保證了得到的子序列中的元素相對位置與原始字串中各個元素的相對位置是相同的。

相關推薦

python實現Longest Common Subsequence公共序列演算法

    最長公共子序列是很基本的演算法,只是最近用到了就又拿來學習一下,網上有很多很多的Java版本的,的確寫的也很不錯,思想都很好,大致上分為三種: 1.基於遞迴的思想 2.基於動態規劃的思想 3.基於HashMap的動態規劃     在這裡我使用的是python來實現,

【LeetCode】Longest Common Subsequence公共序列(求出某一解+LCS長度)

Longest Common Subsequence 給出兩個字串,找到最長公共子序列(LCS),返回LCS的長度。 說明 最長公共子序列的定義: • 最長公共子序列問題是在一組序列(通常2個)中找到最長公共子序列(注意:不同於子串,LCS不需要是

POJ2533(Longest Ordered Subsequence 公共序列 DP或單調佇列+二分)

Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34454 Acce

動態規劃之longest common subsequence公共序列

這題相對於longest common substring而言更容易一些,區別就子序列和子串, 串的話每個字母是要連續的,序列的話,不要求,用動態規劃做,遞推公式如下,不難: 上程式碼: public class CommonSubseq { public stat

POJ 1458 Common Subsequence(公共序列LCS)

題意:        給你兩個字串, 要你求出兩個字串的最長公共子序列長度. 分析:        本題不用輸出子序列,很簡單,直接處理即可.        首先令dp[i][j]==x表示A串

Common Subsequence (公共序列裸題)

Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) lef

python實現公共序列演算法(找到所有公共串)

軟體安全的一個小實驗,正好複習一下LCS的寫法。 實現LCS的演算法和演算法導論上的方式基本一致,都是先建好兩個表,一個儲存在(i,j)處當前最長公共子序列長度,另一個儲存在(i,j)處的回溯方向。 相對於演算法導論的版本,增加了一個多分支回溯,即儲存回溯方向時出現了向上向左都可以的情況時,這時候就代表可能

[LeetCode] Longest Harmonious Subsequence 和諧序列

wiki ray note maximum mon ren enc imu max We define a harmonious array is an array where the difference between its maximum value and

LeetCode 300. Longest Increasing Subsequence —— 上升序列(Java)

什麽 || 序列 無法 tput while 多少 需要 con Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input:

[leetcode]300. Longest Increasing Subsequence遞增序列

rip imp complex sorted nec 序列 lan ted pla Given an unsorted array of integers, find the length of longest increasing subsequence. Examp

Leetcode 300 Longest Increasing Subsequence 遞增序列

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The l

Longest Ordered Subsequence (遞增序列

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN)

300.Longest Increasing Subsequence 遞增序列 動態規劃

題目 給定一個未排序的整數陣列,找到最長遞增子序列的長度。 思路 動態規劃,使用一個數組dp記錄原陣列每一個位置的數字和到這個位置為止的最長子序列長度,dp陣列元素是元組——(a:當前位置最長子序列長度,b:當前位置數字),遍歷陣列,每遍歷到一個數字i,找到

Longest Ordered Subsequence(上升序列,dp)

A numeric sequence of  ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequenc

[LeetCode] Longest Increasing Subsequence 遞增序列

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation:

[LintCode] Longest Increasing Subsequence 遞增序列

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Have you met this question i

每日三題-Day5-A(POJ 2533 Longest Ordered Subsequence 上升序列O(nlogn)解法)

Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51451 Accepted: 22885 Description A numeric

LeetCode - 673. Number of Longest Increasing Subsequence(遞增序列的個數)

LeetCode - 673. Number of Longest Increasing Subsequence(最長遞增子序列的個數) 題目連結 題目 解析 做這題之前先要知道求一個數組的最長遞增子序列。 做法: 求出最長遞增子序列的長度(max)

LCS(longest common subsequence)(公共序列演算法(模板)

看了幾分寫的相當好的部落格: 下面內容來轉載自上面文章 問題描述 什麼是最長公共子序列呢?好比一個數列 S,如果分別是兩個或多個已知數列的子序列,且是所有符合此條件序列中最長的,則S 稱為已知序列的最長公共子序列。     舉個例子,如:有兩條

公共序列(Longest Common Subsequence,lcs)

/** * LCS演算法 * 一個序列A任意刪除若干個字元得到新序列B,則B叫做A的子序列, * 兩個序列X和Y的公共子序列中,長度最長的那個,定義為X和Y的最長公共子序列 */ public static int max(int a