最長公共子序列(lcs)

1、什麼是最長公共子序列web

   

   什麼是最長公共子序列呢?舉個簡單的例子吧,一個數列S,若分別是兩個或多個已知序列的子序列,且是全部符合條件序列中最長的,則S稱爲已知序列的最長公共子序列。數組


  舉例以下,如:有兩個隨機數列,1 2 3 4 5 6 和 3 4 5 8 9,則它們的最長公共子序列即是:3 4 5。promise


  一直不明白:最長公共子串和最長公共子序列的區別。less

  

   上網查了下,最長公共子串(Longest Common Substirng)和最長公共子序列(Longest Common Subsequence,LCS)的區別爲:子串是串的一個連續的部分,子序列則是從不改變序列的順序,而從序列中去掉任意的元素而得到新的序列;也就是說,子串中字符的位置必須是連續的,子序列則能夠沒必要連續。ui


2、蠻力法this


   蠻力法是解決最長公共子序列問題最容易想到的方法,即對S的每個子序列,檢查是否爲T的子序列,從而肯定它是否爲S和T的公共子序列,而且選出最長的公共子序列。spa

 

   S和T的全部子序列都檢查事後便可求出S和T的最長公共子序列。S的一個子序列相應於下標序列1,2,...,n的一個子序列。所以,S共有2^n個子序列。固然,T也有2^m個子序列。code


   所以,蠻力法的時間複雜度爲O(2^n * 2^m),這但是指數級別的啊。orm


3、動態規劃方法ip


   一、序列str1和序列str2

 

  ·長度分別爲m和n;

  ·建立1個二維數組L[m.n];

    ·初始化L數組內容爲0

    ·m和n分別從0開始,m++,n++循環:

       - 若是str1[m] == str2[n],則L[m,n] = L[m - 1, n -1] + 1;

       - 若是str1[m] != str2[n],則L[m,n] = max{L[m,n - 1],L[m - 1, n]}

    ·最後從L[m,n]中的數字必定是最大的,且這個數字就是最長公共子序列的長度

    ·從數組L中找出一個最長的公共子序列


   二、從數組L中查找一個最長的公共子序列


   i和j分別從m,n開始,遞減循環直到i = 0,j = 0。其中,m和n分別爲兩個串的長度。

  ·若是str1[i] == str2[j],則將str[i]字符插入到子序列內,i--,j--;

  ·若是str1[i] != str[j],則比較L[i,j-1]與L[i-1,j],L[i,j-1]大,則j--,不然i--;(若是相等,則任選一個)

根據上圖,咱們能夠獲得其中公共子串:B C B A 和 B D A B。

例題:

 

Description

In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do. 

Therefore the German government requires a program for the following task: 
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind). 

Your country needs this program, so your job is to write it for us.

Input

The input will contain several test cases. 
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'. 
Input is terminated by end of file.

Output

For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

Sample Input

die einkommen der landwirte sind fuer die abgeordneten ein buch mit sieben siegeln um dem abzuhelfen muessen dringend alle subventionsgesetze verbessert werden # die steuern auf vermoegen und einkommen sollten nach meinung der abgeordneten nachdruecklich erhoben werden dazu muessen die kontrollbefugnisse der finanzbehoerden dringend verbessert werden #

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden
題意:給出兩段文字,求出最長的公共單詞串
思路:LCS問題,只須要開個二維來記錄就行了

代碼:

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char a[35][105],b[35][105],c[35][105];
int dp[105][105],mark[105][105],len1,len2,cnt;

void LCS()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    memset(mark,0,sizeof(mark));
    for(i = 0;i<=len1;i++)
    mark[i][0] = 1;
    for(i = 0;i<=len2;i++)
    mark[0][i] = -1;
    for(i = 1; i<=len1; i++)
    {
        for(j = 1; j<=len2; j++)
        {
            if(!strcmp(a[i-1],b[j-1]))
            {
                dp[i][j] = dp[i-1][j-1]+1;
                mark[i][j] = 0;
            }
            else if(dp[i-1][j]>=dp[i][j-1])
            {
                dp[i][j] = dp[i-1][j];
                mark[i][j] = 1;
            }
            else
            {
                dp[i][j] = dp[i][j-1];
                mark[i][j] = -1;
            }
        }
    }
}

void PrintLCS(int i,int j)
{
    if(!i&&!j)
        return ;
    if(mark[i][j]==0)
    {
        PrintLCS(i-1,j-1);
        strcpy(c[cnt++],a[i-1]);
    }
    else if(mark[i][j]==1)
    {
        PrintLCS(i-1,j);
    }
    else
    {
        PrintLCS(i,j-1);
    }
}

int main()
{
    int i;
    while(~scanf("%s",a[0]))
    {
        len1 = 1;
        while(strcmp(a[len1-1],"#"))
            scanf("%s",a[len1++]);
        len1-=1;
        scanf("%s",b[0]);
        len2 = 1;
        while(strcmp(b[len2-1],"#"))
            scanf("%s",b[len2++]);
        LCS();
        cnt = 0;
        PrintLCS(len1,len2);
        printf("%s",c[0]);
        for(i = 1; i<cnt; i++)
        {
            printf(" %s",c[i]);
        }
        printf("\n");
    }

    return 0;}

相關文章
相關標籤/搜索