ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LCS 求最长公共子序列

2019-09-10 23:00:40  阅读:241  来源: 互联网

标签:LCS sequence int len1 len2 length 序列 最长 dp


最长公共子序列不需要字符连续出现和字串不同

//LCS 求最长公共子串模板题

 Common Subsequence

描述

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. 

输入

 

The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

 

输出

 

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

 

样例输入

 

abcfbc abfcab
programming contest
abcd mnp

样例输出

 

4
2
0

 

#include<bits/stdc++.h>
using namespace std;
string a,b;
int dp[1001][1001], len1, len2;
void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else
                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
        }
    }
}
int main()
{
    while(cin>>a)
    {
        cin>>b;
        memset(dp, 0, sizeof(dp));
        len1 = a.length();
        len2 = b.length();
        lcs(len1, len2);
        cout<<dp[len1][len2]<<endl;
    }
}

//有可能遇到爆内存的情况,在只求长度的情况下可以把dp数组改为只有两个状态;

void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
         e^=1; //切换e为1 和 0;
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[e][j] = dp[e^1][j-1] + val[a[i-1]];
            else
                dp[e][j] = max(dp[e^1][j],dp[e][j-1]);
        }
    }
}

//输出最长公共子串上面的代码不变,根据dp数组状态来决定取哪几个元素

#include<bits/stdc++.h>
using namespace std;
string a,b;
int dp[1001][1001], len1, len2;
void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else
                dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
        }
    }
}
void printflcs()
{
    char c[1001];
    memset(c, 0, sizeof(c));
    int i = len1;int j = len2;int d=0;
    while(i!=0 && j!=0)
    {
        if(a[i-1] == b[j-1])
        {
            c[d++] = a[--i];
            j--;
        }
        else if(dp[i-1][j] < dp[i][j-1])
            j--;
        else if(dp[i][j-1] <= dp[i-1][j])
            i--;
    }
    for(i=d-1; i>=0; i--)
        cout<<c[i];
    cout<<endl;

}
int main()
{
    while(cin>>a)
    {
        cin>>b;
        memset(dp, 0, sizeof(dp));
        len1 = a.length();
        len2 = b.length();
        lcs(len1, len2);
        printflcs();
        cout<<dp[len1][len2]<<endl;
    }
}

 

标签:LCS,sequence,int,len1,len2,length,序列,最长,dp
来源: https://www.cnblogs.com/xbqdsjh/p/11503671.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有