Problem p03165 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dump(c)                                                                \
  {                                                                            \
    for (auto it = c.begin(); it != c.end(); ++it)                             \
      if (it == c.begin())                                                     \
        cout << *it;                                                           \
      else                                                                     \
        cout << ' ' << *it;                                                    \
    cout << endl;                                                              \
  }
#define dumpMap(m)                                                             \
  {                                                                            \
    for (auto it : m)                                                          \
      cout << it.first << "=>" << it.second << ' ';                            \
  }

const int MOD = 1000000007;

const int MAXLEN = 3000;
short dp[MAXLEN][MAXLEN];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  string s, t;
  cin >> s >> t;

  int slen = s.length();
  int tlen = t.length();

  // is = 0
  for (int it = 0; it < tlen; ++it) {
    if (s[0] == t[it]) {
      for (; it < tlen; ++it) {
        dp[0][it] = 1;
      }
    } else {
      dp[0][it] = 0;
    }
  }

  //! it = 0
  for (int is = 0; is < slen; ++is) {
    if (s[is] == t[0]) {
      for (; is < slen; ++is) {
        dp[is][0] = 1;
      }
    } else {
      dp[is][0] = 0;
    }
  }

  for (int is = 1; is < slen; ++is) {
    for (int it = 1; it < tlen; ++it) {
      if (s[is] == t[it]) {
        dp[is][it] = dp[is - 1][it - 1] + 1;
      } else {
        dp[is][it] = max(dp[is - 1][it], dp[is][it - 1]);
      }
    }
  }

  string ans;
  int it = tlen - 1;
  int is = slen - 1;
  while (0 < dp[is][it] && 0 <= is && 0 <= it) {
    while (0 < it && dp[is][it - 1] == dp[is][it])
      --it;
    while (0 < is && dp[is - 1][it] == dp[is][it])
      --is;
    ans = s[is] + ans;
    --is;
    --it;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dump(c)                                                                \
  {                                                                            \
    for (auto it = c.begin(); it != c.end(); ++it)                             \
      if (it == c.begin())                                                     \
        cout << *it;                                                           \
      else                                                                     \
        cout << ' ' << *it;                                                    \
    cout << endl;                                                              \
  }
#define dumpMap(m)                                                             \
  {                                                                            \
    for (auto it : m)                                                          \
      cout << it.first << "=>" << it.second << ' ';                            \
  }

const int MOD = 1000000007;

const int MAXLEN = 3000;
short dp[MAXLEN][MAXLEN];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  string s, t;
  cin >> s >> t;

  int slen = s.length();
  int tlen = t.length();

  // is = 0
  for (int it = 0; it < tlen; ++it) {
    if (s[0] == t[it]) {
      for (; it < tlen; ++it) {
        dp[0][it] = 1;
      }
    } else {
      dp[0][it] = 0;
    }
  }

  //! it = 0
  for (int is = 0; is < slen; ++is) {
    if (s[is] == t[0]) {
      for (; is < slen; ++is) {
        dp[is][0] = 1;
      }
    } else {
      dp[is][0] = 0;
    }
  }

  for (int is = 1; is < slen; ++is) {
    for (int it = 1; it < tlen; ++it) {
      if (s[is] == t[it]) {
        dp[is][it] = dp[is - 1][it - 1] + 1;
      } else {
        dp[is][it] = max(dp[is - 1][it], dp[is][it - 1]);
      }
    }
  }

  string ans;
  int it = tlen - 1;
  int is = slen - 1;
  while (0 <= is && 0 <= it && 0 < dp[is][it]) {
    while (0 < it && dp[is][it - 1] == dp[is][it])
      --it;
    while (0 < is && dp[is - 1][it] == dp[is][it])
      --is;
    ans = s[is] + ans;
    --is;
    --it;
  }

  cout << ans << endl;

  return 0;
}