为啥用动态数组会RE啊

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

Anguei @ 2017-11-09 19:19:23

···

#include <iostream>
#include <algorithm>
using namespace std;
//int p1[1005], p2[1005], f[1005][1005];
//int *p1 = new int[100005], *p2 = new int[100005], **f = new int*[100005];
int *p1, *p2, **f;
int main()
{
    int n;
    cin >> n;
    p1 = new int[n + 1], p2 = new int[n + 1], f = new int*[n + 1];
    for (int i = 1; i <= n; ++i)
        f[i] = new int[n + 1];
    for (int i = 0; i <= n; ++i)
        for (int j = 0; j <= n; ++j)
            f[i][j] = 0;
    for (int i = 1; i <= n; ++i)
        cin >> p1[i];
    for (int i = 1; i <= n; ++i)
        cin >> p2[i];
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            if (p1[i] == p2[j])
                f[i][j] = f[i - 1][j - 1] + 1;
            else
                f[i][j] = max(f[i - 1][j], f[i][j - 1]);
    /*for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
            cout << f[i][j] << " ";
        cout << endl;
    }*/
    cout << f[n][n] << endl;
    return 0;
}

··· RE了十个点


by Hades18 @ 2017-11-09 19:37:49

1.不要动态申请内存

2.去洛谷在线IDE上跑数据


by 蹲在丛中笑 @ 2017-11-09 19:49:58

yyk is so strong


by char32_t @ 2017-11-21 14:11:54

简直绝望#include<cstdio>

#include<iostream>
char s[100010], t[100010];
int dp[10001][10001];
int main() {
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%d", &s[i]);
    for(int i=0; i<n; i++) scanf("%d", &t[i]);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1;
            else dp[i+1][j+1]=std::max(dp[i][j+1], dp[i+1][j]);
    printf("%d", dp[n][n]);
    return 0;
}

by char32_t @ 2017-11-21 14:14:42

五彩斑斓

1

AC 0ms/2140KB

2

RE

3

AC 0ms/2234KB

4

AC 0ms/2976KB

5

WA

6

WA

7

MLE

8

RE

9

RE

10

RE


by cosmicAC @ 2018-08-27 20:07:00

您的空间是n^2的,还能不RE


by k1saki @ 2019-09-28 15:18:49

考古


|