定义大小为2d的数组时出现运行时错误(n * m)> = 10 ^ 6

时间:2016-06-16 11:57:23

标签: arrays c++11 runtime-error

这是关于hackerrank“https://www.hackerrank.com/contests/huprog16/challenges/palindromiccrypt”的问题的答案 下面给出的代码在我的电脑上工作正常。但是当我在hackrrank网上运行它给了我“运行时错误”。我最初认为,这是由于2d数组的大尺寸( L [] [] sum [] [] )。所以在阅读Large 2D array gives segmentation fault后使它们失去了功能。它仍然显示hackerrank的运行时错误。

    #include <stdio.h>
    #include <string.h>
    #include <bits/stdc++.h>
    using namespace std;
    #define MAX_VALUE 10000
    // A utility function to get max of two integers
    int max (int x, int y) { return (x > y)? x : y; }
     int L[MAX_VALUE][MAX_VALUE]; // Create a table to store results of subproblems
     int sum[MAX_VALUE][MAX_VALUE];

    int lps(char *str,int n)
    {
    /*int n = strlen(str);*/
    int i, j, cl;

    // Strings of length 1 are palindrome of lentgh 1
    for (i = 0; i < n; i++)
        {L[i][i] = 1; }

    for (i = 0; i < n; i++)
        {
            for (int j = 0; j < n; ++j)
            sum[i][j]=0;
        }

         for (j=1; j<=n; j++)
        {
          for (i=j-1; i>=0; i--)
          {
            if (str[i] == str[j] && i == j-1)
              {L[i][j] = 2; sum[i][j] = 2*(int) str[i];}
            else if (str[i] == str[j])
              {L[i][j] = L[i+1][j-1] + 2; sum[i][j] = sum[i+1][j-1] + 2*(int)str[i]; }
            else
              {L[i][j] = max(L[i][j-1], L[i+1][j]); sum[i][j] = max(L[i][j-1], L[i+1][j]) == L[i][j-1]?sum[i][j-1]:sum[i+1][j];}
          }
        }
        return sum[0][n-1];
    }


    int main()
    {
        char seq[MAX_VALUE];
        int n; cin>>n;
        for (int i = 0; i < n; ++i)
                cin>>seq[i];
       // int n = strlen(seq);

        cout<<lps(seq,n);

        return 0;
    }

1 个答案:

答案 0 :(得分:-1)

尝试一下

int hourglassSum(vector<vector<int>> arr)
{
 int r=6,c=6;
 int i,j;
 int sum;
 int temp;
 int max=-100;
  for(i=0;i<r-2;i++)
  {
    for(j=0;j<c-2;j++)
    {
      sum=(arr[i][j]+arr[i][j+1]+arr[i][j+2])+(arr[i+1][j+1])+(arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]);
      if(max<sum)
      max=sum;
  }
  
 }
 return max;
}