POJ 1458 最長公共子序列dp

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <math.h>
#define eps 1e-14
#define pi acos(-1)
#define ll long long
#define RD T*(rand()*2-RAND_MAX)
#define Drand (long double)rand()/RAND_MAX
#define LINF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1005;
const long long mod=1e18;
ll MOD(ll a,ll m){return a>m?a%m+m:a;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

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


int main()
{
//    freopen("in.txt","r",stdin);
    while(~scanf("%s%s",a+1,b+1)){
        memset(dp,0,sizeof dp);
        a[0]=1;
        b[0]=1;
        int lena=strlen(a);
        int lenb=strlen(b);
        for(int i=1;i<lena;i++){
            for(int j=1;j<lenb;j++){
                if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;
                else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        printf("%d\n",dp[lena-1][lenb-1]);
    }
    return 0;
}

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