51nod 1006 最长公共子序列Lcs

51nod 1006 最长公共子序列Lcs

Ax 表示 A序列中1–x的子序列
By 表示 B序列中1–y的子序列
用 L(x, y) 表示Ax子序列和By子序列的一个最长公共子序列。
用LCS(x, y) 表示Ax子序列和By子序列的一个最长公共子序列的长度。


序列长度递归方程
LCS(x, y) =
(1) LCS(x - 1,y - 1) + 1 如果Ax = By
(2) max(LCS(x – 1, y) , LCS(x, y – 1)) 如果Ax ≠ By
(3) 0 如果x = 0或者y = 0

序列递归方程
L(x, y) =
(1) LCS(x – 1, y – 1) + 1如果Ax = By
这对应L(x,y) = L(x,- 1 y- 1)末尾接上Ax

(2.1) LCS(x – 1, y) 如果Ax ≠ By且LCS(x – 1, y) >=LCS(x, y – 1)
这对应L(x,y) = L(x – 1, y)
(2.2) LCS(x, y – 1) 如果Ax ≠ By且LCS(x – 1, y) < LCS(x, y – 1)
这对应L(x,y) = L(x, y – 1)

(3) 0 如果 x =0或者y = 0
这对应L(x,y)=空序列


图1
图1

以下计算最长公共子序列的长度,并描述了图1。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define E 2.71828
#define MOD 1000000007
#define N 1010
char a[N],b[N];
int dp[N][N];
int main()
{
    int n;
    scanf("%s%s",a+1,b+1);
    a[0] = b[0] = ' ';
    int lena = strlen(a);
    int lenb = strlen(b);
    for(int i = 0; i < lena; i++)
    {
        for(int j = 0; j < lenb; j++)
        {
            if(i == 0 || j == 0)
                dp[i][j] = 0;
            else if(a[i] == b[j])
                dp[i][j] = dp[i-1][j-1]+1;
            else
                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
        }
    }
    for(int i = 0; i < lena; i++)
    {
        for(int j = 0; j < lenb; j++)
            printf("%2d",dp[i][j]);
        puts("");
    }
    printf("%d\n",dp[lena-1][lenb-1]);
    return 0;
}

运行结果比较图1
这里写图片描述


求出长度后,如何寻找这段路径?
答案: 怎么来的,怎么回去,逆推下就能找到,但路径不止一条。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define E 2.71828
#define MOD 1000000007
#define N 1010
char a[N],b[N];
int dp[N][N];
char output[N];
int main()
{
    int n;
    scanf("%s%s",a+1,b+1);
    a[0] = b[0] = ' ';
    int lena = strlen(a);
    int lenb = strlen(b);
    for(int i = 0; i < lena; i++)
    {
        for(int j = 0; j < lenb; j++)
        {
            if(i == 0 || j == 0)
                dp[i][j] = 0;
            else if(a[i] == b[j])
                dp[i][j] = dp[i-1][j-1]+1;
            else
                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
        }
    }
    int i = lena-1;
    int j = lenb-1;
    int len = dp[lena-1][lenb-1] - 1;
    while(dp[i][j] != 0)
    {
        if(a[i] == b[j])
        {
            output[len--] = a[i];
            i--;
            j--;
        }
        else
        {
            if(dp[i-1][j] == dp[i][j])
                i--;
            else
                j--;
        }
    }
    output[dp[lena-1][lenb-1]] = '\0';
    printf("%s",output);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值