Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/* Dynamic Programming implementation of LCS problem */
#include <bits/stdc++.h>
using namespace std;

// Maximum string length
#define N 100

int dp[N][N];

string printLCS(string X, int m, int n) {
  string s = "";

  while (0 < m and 0 < n) {
    if (dp[m][n] == dp[m][n - 1])
      n--; // daaner tar soman hole
    else if (dp[m][n] == dp[m - 1][n])
      m--; // uporer tar soman hole
    else {
      s += X[m -
             1]; // or i can add s[n-1], mane eikhane char 2 ta milche. eikhane
                 // diagonal er sathe 1 add hoiche. ar ekhn e else ken? karon ei
                 // table e value kokhonoi kome na. same thake or bare.
      m--, n--;
    }
  }

  reverse(s.begin(), s.end()); // hisab ta kora hoy last theke, tai reverse kora

  return s;
}

// Returns length of LCS for X[0..m-1], Y[0..n-1]; building the table, also LCS
// size can be found from this table
void LCStable(string X, string Y, int m, int n) {
  // Build L[m+1][n+1] in bottom up fashion
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 ||
          j == 0) // i/j 0 hole 0-1 index bole to kichu nai. tai 0 hobe.
        dp[i][j] = 0;
      else if (X[i - 1] ==
               Y[j - 1]) // char mile gele diagonal er sathe 1 + hobe
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] =
            max(dp[i - 1][j],
                dp[i][j - 1]); // na mille daner ba uporer tar moddhe max ta
    }
  }
  // return dp[m][n]; // number of the last row and last column ekdom nicer
  // daner value ta. the max value in the table!
}

/* Driver program to test above function */
int main() {
  string X, Y;
  cin >> X >> Y;
  int m = X.length();
  int n = Y.length();

  LCStable(X, Y, m, n);
  cout << printLCS(X, m, n) << endl;

  return 0;
}


Fixed submission:
/* Dynamic Programming implementation of LCS problem */
#include <bits/stdc++.h>
using namespace std;

// Maximum string length
#define N 3000 + 1

int dp[N][N];

string printLCS(string X, int m, int n) {
  string s = "";

  while (0 < m and 0 < n) {
    if (dp[m][n] == dp[m][n - 1])
      n--; // daaner tar soman hole
    else if (dp[m][n] == dp[m - 1][n])
      m--; // uporer tar soman hole
    else {
      s += X[m -
             1]; // or i can add s[n-1], mane eikhane char 2 ta milche. eikhane
                 // diagonal er sathe 1 add hoiche. ar ekhn e else ken? karon ei
                 // table e value kokhonoi kome na. same thake or bare.
      m--, n--;
    }
  }

  reverse(s.begin(), s.end()); // hisab ta kora hoy last theke, tai reverse kora

  return s;
}

// Returns length of LCS for X[0..m-1], Y[0..n-1]; building the table, also LCS
// size can be found from this table
void LCStable(string X, string Y, int m, int n) {
  // Build L[m+1][n+1] in bottom up fashion
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 ||
          j == 0) // i/j 0 hole 0-1 index bole to kichu nai. tai 0 hobe.
        dp[i][j] = 0;
      else if (X[i - 1] ==
               Y[j - 1]) // char mile gele diagonal er sathe 1 + hobe
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] =
            max(dp[i - 1][j],
                dp[i][j - 1]); // na mille daner ba uporer tar moddhe max ta
    }
  }
  // return dp[m][n]; // number of the last row and last column ekdom nicer
  // daner value ta. the max value in the table!
}

/* Driver program to test above function */
int main() {
  string X, Y;
  cin >> X >> Y;
  int m = X.length();
  int n = Y.length();

  LCStable(X, Y, m, n);
  cout << printLCS(X, m, n) << endl;

  return 0;
}