Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 3121000;
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int rev[3100][3100];
int dp[3100][3100];
char s[3100];
char t[3100];
char ans[3100];
int main() {
  scanf("%s%s", s, t);
  int N = strlen(s);
  int M = strlen(t);
  for (int i = 0; i < 3100; i++)
    for (int j = 0; j < 3100; j++)
      dp[i][j] = 0;
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (i + 1 <= N) {
        if (dp[i + 1][j] < dp[i][j]) {
          dp[i + 1][j] = dp[i][j];
          rev[i + 1][j] = 0;
        }
      }
      if (j + 1 <= M) {
        if (dp[i][j + 1] < dp[i][j]) {
          dp[i][j + 1] = dp[i][j];
          rev[i][j + 1] = 1;
        }
      }
      if (i + 1 <= N && j + 1 <= M && s[i] == t[j]) {
        if (dp[i + 1][j + 1] < dp[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          rev[i + 1][j + 1] = 2;
        }
      }
    }
  }
  int ns = N;
  int nt = M;
  int at = dp[N][M] - 1;
  while (ns || nt) {
    if (rev[ns][nt] == 0)
      ns--;
    else if (rev[ns][nt] == 1)
      nt--;
    else {
      ns--;
      nt--;
      ans[at] = s[ns];
      at--;
    }
  }
  printf("%s\n", ans);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 3121000;
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int rev[3100][3100];
int dp[3100][3100];
char s[3100];
char t[3100];
char ans[3100];
int main() {
  scanf("%s%s", s, t);
  int N = strlen(s);
  int M = strlen(t);
  for (int i = 0; i < 3100; i++)
    for (int j = 0; j < 3100; j++)
      dp[i][j] = -3100;
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (i + 1 <= N) {
        if (dp[i + 1][j] < dp[i][j]) {
          dp[i + 1][j] = dp[i][j];
          rev[i + 1][j] = 0;
        }
      }
      if (j + 1 <= M) {
        if (dp[i][j + 1] < dp[i][j]) {
          dp[i][j + 1] = dp[i][j];
          rev[i][j + 1] = 1;
        }
      }
      if (i + 1 <= N && j + 1 <= M && s[i] == t[j]) {
        if (dp[i + 1][j + 1] < dp[i][j] + 1) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          rev[i + 1][j + 1] = 2;
        }
      }
    }
  }
  int ns = N;
  int nt = M;
  int at = dp[N][M] - 1;
  while (ns || nt) {
    if (rev[ns][nt] == 0)
      ns--;
    else if (rev[ns][nt] == 1)
      nt--;
    else {
      ns--;
      nt--;
      ans[at] = s[ns];
      at--;
    }
  }
  printf("%s\n", ans);
}