最长公共子序列问题(LCS)

来源:互联网 发布:单位换算软件 编辑:程序博客网 时间:2024/05/23 14:28

最长公共子序列问题(LCS)

代码块

#include<iostream>#include<cstring>#define MAX 1000using namespace std;int c[MAX][MAX];int dp(int m, int n, char* x, char* y){    for(int j=0; j<=n; j++)        c[0][j] = 0;    for(int i=0; i<=m; i++)        c[i][0] = 0;    for(int i=1; i<=m; i++)        for(int j=1; j<=n; j++)        {            if(x[i-1] == y[j-1])                c[i][j] = c[i-1][j-1] + 1;            else if(c[i][j-1] > c[i-1][j])                c[i][j] = c[i][j-1];            else                c[i][j] = c[i-1][j];        }        return c[m][n]; } int main(){    char x[MAX], y[MAX];    cin >> x >> y;    cout << dp(strlen(x), strlen(y), x, y) << endl;    return 0;}