码迷,mamicode.com
首页 > 其他好文 > 详细

最长公共子序列Lcs

时间:2020-02-02 17:52:20      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:cin   else   output   lse   using   递归   col   main   逆向   

给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的)。

比如两个串为:
 
abcicba
abdkscab
 
ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符串最长的子序列。

Input

第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000)

Output

输出最长的子序列,如果有多个,随意输出1个。

Sample Input

abcicba
abdkscab

Sample Output

abca
基础的lcs,要求输出任意一个最长公共子序列
写一个递归函数模拟之前寻找最长公共子序列的逆向过程,把每次取到的字符入栈,然后再输出就好了
#include<iostream>
#include<string>
#include<stack>
using namespace std;

int max(int a, int b) {
    return a > b ? a : b;
}

int map[1001][1001] = { 0 };
string str1, str2;
stack<char> str;

void fun(int i, int j) {
    if (i == 0 || j == 0) {
        return;
    }
    if (str1[i - 1] == str2[j - 1]) {
        str.push(str1[i - 1]);
        fun(i - 1, j - 1);
    }
    else {
        if (map[i - 1][j] > map[i][j - 1]) {
            fun(i - 1, j);
        }
        else {
            fun(i, j - 1);
        }
    }

}

int main() {

    cin >> str1 >> str2;
    for (int i = 0; i < str1.length(); i++) {
        for (int j = 0; j < str2.length(); j++) {
            if (str1[i] == str2[j]) {
                map[i + 1][j + 1] = map[i][j] + 1;;
            }
            else {
                map[i + 1][j + 1] = max(map[i][j + 1], map[i + 1][j]);
            }
        }
    }
    fun(str1.length(), str2.length());


    while (!str.empty()) {
        cout << str.top();
        str.pop();
    }

    return 0;
}

 

最长公共子序列Lcs

标签:cin   else   output   lse   using   递归   col   main   逆向   

原文地址:https://www.cnblogs.com/Vetsama/p/12252787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!