最大公共子序列

#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 1<<30;
const int MAXN = 1000;

int dp[1010][1010];
char a[1010],b[1010];

int main()
{
    while(cin >> a+1 >> b+1)
    {
        memset(dp,0,sizeof(dp));
        int len1 = strlen(a+1),len2 = strlen(b+1);

        for(int i = 1 ; i <= len1 ; i++)
            for(int j = 1 ; j <= len2 ; j++)
                dp[i][j] = max((const int)(dp[i-1][j-1] + (a[i] == b[j])) , max(dp[i-1][j],dp[i][j-1]));

        cout << dp[len1][len2] << endl;
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章