求最长公共子序列Longest Common Subsequence LCS

最长公共子序列:一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列。最长公共子序列与最长公共子串(要求连续)是不同的。

比如ADE和ABCDE的最长公共子序列是ADE。

复杂度:对于一般性的LCS问题(即任意数量的序列)是属于NP-hard。但当序列的数量确定时,问题可以使用动态规划(Dynamic Programming)在多项式时间解决。

最长公共子序列是一个十分实用的问题,它可以描述两段文字之间的“相似度”,即它们的雷同程度,从而能够用来辨别抄袭。对一段文字进行修改之后,计算改动前后文字的最长公共子序列,将除此子序列外的部分提取出来,这种方法判断修改的部分,往往十分准确。

注意:最长公共序列的定义中,并不要求最长公共子序列必须连续出现在两个字符串中,只需要能保持顺序的出现在序列中即可.

算法:

动态规划的一个计算最长公共子序列的方法如下,以两个序列 X、Y 为例子:
设有二维数组 f[i][j] 表示 X 的 i 位和 Y 的 j 位之前的最长公共子序列的长度,则有:
f[1][1] = same(1,1)
f[i][j] = max\{f[i-1][j-1] + same(i,j), f[i-1][j],f[i][j-1]\}
其中,same(a,b)当 X 的第 a 位与 Y 的第 b 位完全相同时为“1”,否则为“0”。
此时,f[i][j]中最大的数便是 X 和 Y 的最长公共子序列的长度,依据该数组回溯,便可找出最长公共子序列。
该算法的空间、时间复杂度均为O(n^{2}),经过优化后,空间复杂度可为O(n),时间复杂度为O(n\log n)。

code

在递归过程中使用dp记录已经计算过的状态。

dp[i][j]记录的是:str1以i位置开始的字符串,str2以j为位置开始的字符串的最大公共子序列。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1001;
int dp[N][N];
int lcs_seq(char *str1,int b1,int la1,char *str2,int b2,int la2){
    if(b1>la1||b2>la2)
        return 0;
    int t1,t2,t3;
    if(dp[b1+1][b2]==-1){
        t1=lcs_seq(str1,b1+1,la1,str2,b2,la2);
        dp[b1+1][b2]=t1;
    }
    else
        t1=dp[b1+1][b2];
    if(dp[b1][b2+1]==-1){
        t2=lcs_seq(str1,b1,la1,str2,b2+1,la2);
        dp[b1][b2+1]=t2;
    }
    else
        t2=dp[b1][b2+1];
    int same=0;
    if(str1[b1]==str2[b2])
        same=1;
    if(dp[b1+1][b2+1]==-1){
        t3=lcs_seq(str1,b1+1,la1,str2,b2+1,la2)+same;
        dp[b1+1][b2+1]=t3;
    }
    else
        t3=dp[b1+1][b2+1]+same;
    t1=max(t1,t2);
    t1=max(t1,t3);
    dp[b1][b2]=t1;
    return t1;
}
int main()
{
    int n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        char str1[N],str2[N];
        scanf("%s%s",str1,str2);
        memset(dp,-1,sizeof(dp));
        int sum=lcs_seq(str1,0,strlen(str1)-1,str2,0,strlen(str2)-1);
        printf("%d\n",sum);
    }
    return 0;
}


str1[i]==str2[i],

那么,它的子问题就是dp[i+1][j+1];

否则需要判断哪个子问题可以返回更大的子串。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
const int N=1001;
int dp[N][N];
int lcs_seq(char *str1,int b1,int la1,char *str2,int b2,int la2){
    if(b1>la1||b2>la2)
        return 0;
    int t1,t2,t3;
    if(dp[b1+1][b2]==-1){
        t1=lcs_seq(str1,b1+1,la1,str2,b2,la2);
        dp[b1+1][b2]=t1;
    }
    else
        t1=dp[b1+1][b2];
    if(dp[b1][b2+1]==-1){
        t2=lcs_seq(str1,b1,la1,str2,b2+1,la2);
        dp[b1][b2+1]=t2;
    }
    else
        t2=dp[b1][b2+1];
    int same=0;
    if(str1[b1]==str2[b2])
        same=1;
    if(dp[b1+1][b2+1]==-1){
        t3=lcs_seq(str1,b1+1,la1,str2,b2+1,la2)+same;
        dp[b1+1][b2+1]=t3;
    }
    else
        t3=dp[b1+1][b2+1]+same;
    t1=max(t1,t2);
    t1=max(t1,t3);
    dp[b1][b2]=t1;
    return t1;
}
void print_lcs(int i,int len1,int j,int len2,char *str1,char *str2)
{
    if(i<len1&&j<len2){
       if(str1[i]==str2[j])
       {
           printf("%c",str1[i]);
           print_lcs(i+1,len1,j+1,len2,str1,str2);
       }
       else if(dp[i+1][j]>=dp[i][j+1])
           print_lcs(i+1,len1,j,len2,str1,str2);
       else
           print_lcs(i,len1,j+1,len2,str1,str2);
    }
}
int main()
{
    int n,i;
    char str1[N],str2[N];
    while(scanf("%s%s",str1,str2)!=EOF){
        memset(dp,-1,sizeof(dp));
        int sum=lcs_seq(str1,0,strlen(str1)-1,str2,0,strlen(str2)-1);
        printf("%d\n",sum);
        print_lcs(0,strlen(str1),0,strlen(str2),str1,str2);
        printf("\n");
    }
    return 0;
}

将递归改为循环,减少了程序中一些判断。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
const int N=1001;
int dp[N+1][N+1];
int lcs_seq(char *str1,int len1,char *str2,int len2){
   int i,j;
   for(i=len1-1;i>=0;i--)
       for(j=len2-1;j>=0;j--){
           if(str1[i]=='\0'||str2[j]=='\0')
               dp[i][j]=0;
           else if(str1[i]==str2[j])
               dp[i][j]=dp[i+1][j+1]+1;
           else 
               dp[i][j]=max(dp[i+1][j],dp[i][j+1]);
       } 
   return dp[0][0];
}
void print_lcs(int i,int len1,int j,int len2,char *str1,char *str2)
{
    if(i<len1&&j<len2){
       if(str1[i]==str2[j])
       {
           printf("%c",str1[i]);
           print_lcs(i+1,len1,j+1,len2,str1,str2);
       }
       else if(dp[i+1][j]>=dp[i][j+1])
           print_lcs(i+1,len1,j,len2,str1,str2);
       else
           print_lcs(i,len1,j+1,len2,str1,str2);
    }
}
int main()
{
    int n,i;
    char str1[N],str2[N];
    while(scanf("%s%s",str1,str2)!=EOF){
        memset(dp,0,sizeof(dp));
        int sum=lcs_seq(str1,strlen(str1),str2,strlen(str2));
        printf("%d\n",sum);
        print_lcs(0,strlen(str1),0,strlen(str2),str1,str2);
        printf("\n");
    }
    return 0;
}


例题:

最长公共子序列:http://acm.nyist.net/JudgeOnline/problem.php?pid=36

poj:http://poj.org/problem?id=1458

参考文献:

维基:http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离:http://www.cnblogs.com/zhangchaoyang/articles/2012070.html

算法讲解:http://www.ics.uci.edu/~eppstein/161/960229.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值