Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset
// tree<int, null_type,less_equal<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define jai_shree_ram                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b, d) for (int i = a; i <= b; i += d)
#define brep(i, a, b, d) for (int i = a; i >= b; i -= d)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define endl '\n'
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
////////////////////////////////

pair<int, pii> dp[3005][3005];

int32_t main() {
  jai_shree_ram string s, t;
  cin >> s >> t;
  int l1 = s.length(), l2 = t.length();

  rep(i, 1, l1, 1) {
    rep(j, 1, l2, 1) {
      if (dp[i - 1][j].first >= dp[i][j - 1].first) {
        dp[i][j] = dp[i - 1][j];
      } else
        dp[i][j] = dp[i][j - 1];

      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = {dp[i - 1][j - 1].first + 1, {i, j}};
      }
    }
  }
  string ans = "";
  int i = l1, j = l2;
  while (1) {
    int i1 = (dp[i][j].second).first;
    int j1 = (dp[i][j].second).second;
    // cout << i1 << " " << j1 << endl;
    ans += s[i1 - 1];
    if (i1 == 1)
      break;
    i = i1 - 1;
    j = j1 - 1;
  }
  reverse(all(ans));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset
// tree<int, null_type,less_equal<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define jai_shree_ram                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b, d) for (int i = a; i <= b; i += d)
#define brep(i, a, b, d) for (int i = a; i >= b; i -= d)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define endl '\n'
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
////////////////////////////////

pair<int, pii> dp[3005][3005];

int32_t main() {
  jai_shree_ram string s, t;
  cin >> s >> t;
  int l1 = s.length(), l2 = t.length();

  rep(i, 1, l1, 1) {
    rep(j, 1, l2, 1) {
      if (dp[i - 1][j].first >= dp[i][j - 1].first) {
        dp[i][j] = dp[i - 1][j];
      } else
        dp[i][j] = dp[i][j - 1];

      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = {dp[i - 1][j - 1].first + 1, {i, j}};
      }
    }
  }
  string ans = "";
  int i = l1, j = l2;
  while (1) {
    int i1 = (dp[i][j].second).first;
    int j1 = (dp[i][j].second).second;
    if (i1 == 0)
      break;
    ans += s[i1 - 1];
    if (i1 == 1)
      break;
    i = i1 - 1;
    j = j1 - 1;
  }
  reverse(all(ans));
  cout << ans << endl;
  return 0;
}