50pts崩溃求调
查看原帖
50pts崩溃求调
1152158
Code_Rime楼主2024/10/22 11:39

不知道哪里错了,在力扣是过的

//力扣1143
#include<iostream>
using namespace std;
#define int long long
#define maxn 300010
int text1[maxn],text2[maxn],n;
int longestCommonSubsequence(int text1[], int text2[])
{
        int dp[n+1][n+1];
        dp[0][0] = text1[0] == text2[0] ? 1 : 0;
        for(int i = 1;i < n;i++) 
        {
            if(text1[i] != text2[0] && dp[0][i-1] == 0) 
                dp[0][i] = 0;
            else 
                dp[0][i] = 1;
        }
        for(int i = 1;i < n;i++) 
        {
            if(text2[i] != text1[0] && dp[i-1][0] == 0) 
                dp[i][0] = 0;
            else
                dp[i][0] = 1;            
            for(int j = 1;j < n;j++) 
            {
                if(text2[i] == text1[j]) 
                    dp[i][j] = dp[i-1][j-1] + 1;
                else 
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);                
            }
        }
        return dp[n-1][n-1];
    }
main()
{
    cin >> n;
    for(int i = 0; i < n ;i++)
        cin >> text1[i];
    for(int i = 0; i < n ;i++)
        cin >> text2[i];
    cout << longestCommonSubsequence(text1,text2);
}
2024/10/22 11:39
加载中...