Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define int long long
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define fone for (i = 0; i < n; i++)
#define endl "\n"
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 1;
const ll N2 = 1000 * 1000 + 5;
const ld PI = 3.14159265;
const ll R = 250;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

signed main() {
  fastio();
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> dp(3000, vector<ll>(3000, 0LL));

  for (int i = 1; i <= s.length(); i++) {
    for (int j = 1; j <= t.length(); j++) {
      // cout<<dp[i][j];
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        // cout<<s[i-1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
      // cout<<dp[i][j]<<endl;
    }
  }
  int index = dp[s.length()][t.length()];
  int i = s.length();
  int j = t.length();
  char lcs[index + 1];
  lcs[index] = '\0';
  while (i > 0 && j > 0) {
    // If current character in X[] and Y are same, then
    // current character is part of LCS
    if (s[i - 1] == t[j - 1]) {
      lcs[index - 1] = s[i - 1]; // Put current character in result
      i--;
      j--;
      index--; // reduce values of i, j and index
    }

    // If not same, then find the larger of two and
    // go in the direction of larger value
    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << lcs;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define int long long
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define fone for (i = 0; i < n; i++)
#define endl "\n"
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 1;
const ll N2 = 1000 * 1000 + 5;
const ld PI = 3.14159265;
const ll R = 250;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

signed main() {
  fastio();
  string s, t;
  cin >> s >> t;
  vector<vector<ll>> dp(4000, vector<ll>(4000, 0LL));

  for (int i = 1; i <= s.length(); i++) {
    for (int j = 1; j <= t.length(); j++) {
      // cout<<dp[i][j];
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
        // cout<<s[i-1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
      // cout<<dp[i][j]<<endl;
    }
  }
  int index = dp[s.length()][t.length()];
  int i = s.length();
  int j = t.length();
  char lcs[index + 1];
  lcs[index] = '\0';
  while (i > 0 && j > 0) {
    // If current character in X[] and Y are same, then
    // current character is part of LCS
    if (s[i - 1] == t[j - 1]) {
      lcs[index - 1] = s[i - 1]; // Put current character in result
      i--;
      j--;
      index--; // reduce values of i, j and index
    }

    // If not same, then find the larger of two and
    // go in the direction of larger value
    else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  cout << lcs;
}