请各位大佬看看,dp写法为何输出全0?

P1439 【模板】最长公共子序列

江南小巫 @ 2018-01-28 16:45:12

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define M(a) memset(a,0,sizeof a)
#define fo(j,n) for(i=j;i<=n;i++)
#define MOD 1000000007
#define ll long long
using namespace std;
int a[105],b[105];
int dp[105][105];
int n,i,j;
int main(){
    scanf("%d",&n);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n;i++)scanf("%d",&b[i]);
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            if(a[i]==b[i])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[n][n]);
    return 0;
}

by Wolfycz @ 2018-01-28 17:52:32

你写a[i]==b[i]肯定会输出0啊

应该写a[i]==b[j]


by HuangBo @ 2018-03-15 16:48:21

呵呵一笑———这是纯O(N^2)算法吧...


|