1. 程式人生 > 其它 >最長公共(不連續)子序列(動態規劃)

最長公共(不連續)子序列(動態規劃)

動態規劃(遞推遞迴)

題目

Description

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.

Input

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

Output

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.

Sample Input

abcfbc         abfcab
programming    contest 
abcd           mnp

Sample Output

4
2
0

思想

1. 和其他動態規劃題目一樣,這類題目無疑還是遞迴或者遞推方法+記憶化方法.

2. 因為這是最長的(可以不連續)的最長子序列,所以仍然可以用當前值如果相同則當前組合的最長值可以用上一個值的最長公共子序列+1;如果不相同,則倒回去,分別位置-1,選其中大的值。

程式碼

解法1. 遞迴+記憶化(O(n*m))

/*
-------------------------------------------------
   Author:       wry
   date:         2022/3/5 9:45
   Description:  test
-------------------------------------------------
*/

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

const int MAXN = 1000+10;

char str1[MAXN];
char str2[MAXN];
int str[MAXN][MAXN];    //記錄以str1的i下標結尾和str2的j下標結尾的公共子序列

int Fun(int i,int j) {
    int answer;
    if (i<0 || j<0) {
        return 0;
    }
    if (str[i][j]!=-1) {       //遞迴
        return str[i][j];
    }
    else {
        if (str1[i]==str2[j]) {
            answer = Fun(i-1,j-1) + 1;    //就算是0號位置相同,也能保證Fun(-1,-1)返滬0,+1後為1.
        }
        else {
            answer = max(Fun(i-1,j),Fun(i,j-1));    //如果當前元素不同則返回各自上一個的最大值
        }
        str[i][j] = answer;
    }
    return str[i][j];
}

int main() {
    while (cin >> str1 >> str2) {     //如果是string型別則無法通過~,只有char才可以
        int n = strlen(str1);
        int m = strlen(str2);   
        memset(str,-1,sizeof(str));
        cout << Fun(n-1,m-1) << endl;     //以最後一個元素結束,它的最大公共子序列一定是最大的
    }
    return 0;
}

解法2. 遞推+記憶化(O(n*m))

/*
-------------------------------------------------
   Author:       wry
   date:         2022/3/5 10:30
   Description:  test
-------------------------------------------------
*/

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 1000+10;

char str1[MAXN];
char str2[MAXN];
int str[MAXN][MAXN];    //記錄以str1的i下標結尾和str2的j下標結尾的公共子序列

void Fun(int n,int m) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {     //每次從上到下處理一行
            if (str1[i] == str2[j]) {
                str[i][j] = str[i-1][j-1] + 1;
            } else {
                str[i][j] = max(str[i-1][j], str[i][j-1]);      //如果是i=1時,str[0][j]一定都是0,而str[1][j-1]在這個情況下要麼是已經處理過的了,要麼是str[1][0]也是0,怎麼樣都是對的
            }
        }
    }
}

int main() {
    while (cin >> str1+1 >> str2+1) {     //每個都從下標為1位置開始,0行0列作為哨兵緩衝
        int n = strlen(str1+1);
        int m = strlen(str2+1);      //長度也是從1號下標開始計算
        memset(str,0,sizeof(str));
        Fun(n,m);
        cout << str[n][m] << endl;     //以最後一個元素結束,它的最大公共子序列一定是最大的
    }
    return 0;
}