Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
    [Gnana Deepak]
    [August 01, 2020 7:44 PM]
    [F - LCS]
    [https://atcoder.jp/contests/dp/tasks/dp_f]
    [2000 ms]
    [1024 MB]
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ld = long double;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define int long long
#define F first
#define S second
#define orderKey order_of_key
#define findOrder find_by_order
#define s(x) set<x>
#define um(x, y) unordered_map<x, y>
#define m(x, y) map<x, y>
#define p(x, y) pair<x, y>
#define v(x) vector<x>
#define min_heap(t) priority_queue<t, vector<t>, greater<t>>
#define max_heap(t) priority_queue<t>
#define ordered_set(x)                                                         \
  tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define ss(v) stable_sort(all(v))
#define fori(i, a, b) for (int i = a; i <= b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define stprs(x) cout << fixed << setprecision(x);
#define FILE_READ_IN freopen("input.txt", "r", stdin);
#define FILE_READ_OUT freopen("output.txt", "w", stdout);
#define bs binary_search
#define deb(x) cout << #x << "=" << x << '\n';
#define scn(v1)                                                                \
  for (auto &x : v1)                                                           \
    cin >> x;
#define R(x) reverse(all(x));
#define lb lower_bound
#define ub upper_bound
const char nl = '\n';
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
const ld PI = acos(-1);
const int N = 3000;
const int imx = LLONG_MAX;
int DP[N + 1][N + 1];
void accio_ac(void) {
  string S, T;
  cin >> S >> T;
  int s = S.length();
  int t = T.length();
  fori(i, 0, s) fori(j, 0, t) {
    if (i == 0 or j == 0) {
      DP[i][j] = 0;
    } else {
      if (S[i - 1] == T[j - 1]) {
        DP[i][j] = 1 + DP[i - 1][j - 1];
      } else {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
      }
    }
  }
  stack<char> Stack;
  int i = s - 1;
  int j = t - 1;
  while (i >= 0 and j >= 0) {
    if (S[i] == T[j]) {
      Stack.push(S[i]);
      --i;
      --j;
    } else {
      if (DP[i - 1][j] > DP[i][j - 1]) {
        --i;
      } else {
        --j;
      }
    }
  }
  while (!Stack.empty()) {
    cout << Stack.top();
    Stack.pop();
  }
  cout << endl;
}
int32_t main(void) {
  fastio;
  int __ = 1;
  // cin >> __;
  while (__--) {
    accio_ac();
  }
  return 0;
}


Fixed submission:
/*
    [Gnana Deepak]
    [August 01, 2020 7:44 PM]
    [F - LCS]
    [https://atcoder.jp/contests/dp/tasks/dp_f]
    [2000 ms]
    [1024 MB]
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ld = long double;
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define int long long
#define F first
#define S second
#define orderKey order_of_key
#define findOrder find_by_order
#define s(x) set<x>
#define um(x, y) unordered_map<x, y>
#define m(x, y) map<x, y>
#define p(x, y) pair<x, y>
#define v(x) vector<x>
#define min_heap(t) priority_queue<t, vector<t>, greater<t>>
#define max_heap(t) priority_queue<t>
#define ordered_set(x)                                                         \
  tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define ss(v) stable_sort(all(v))
#define fori(i, a, b) for (int i = a; i <= b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define stprs(x) cout << fixed << setprecision(x);
#define FILE_READ_IN freopen("input.txt", "r", stdin);
#define FILE_READ_OUT freopen("output.txt", "w", stdout);
#define bs binary_search
#define deb(x) cout << #x << "=" << x << '\n';
#define scn(v1)                                                                \
  for (auto &x : v1)                                                           \
    cin >> x;
#define R(x) reverse(all(x));
#define lb lower_bound
#define ub upper_bound
const char nl = '\n';
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
const ld PI = acos(-1);
const int N = 3000;
const int imx = LLONG_MAX;
int DP[N + 1][N + 1];
void accio_ac(void) {
  string S, T;
  cin >> S >> T;
  int s = S.length();
  int t = T.length();
  fori(i, 0, s) fori(j, 0, t) {
    if (i == 0 or j == 0) {
      DP[i][j] = 0;
    } else {
      if (S[i - 1] == T[j - 1]) {
        DP[i][j] = 1 + DP[i - 1][j - 1];
      } else {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
      }
    }
  }
  stack<char> Stack;
  int i = s - 1;
  int j = t - 1;
  while (i >= 0 and j >= 0) {
    if (S[i] == T[j]) {
      Stack.push(S[i]);
      --i;
      --j;
    } else {
      if (DP[i][j + 1] > DP[i + 1][j]) {
        --i;
      } else {
        --j;
      }
    }
  }
  while (!Stack.empty()) {
    cout << Stack.top();
    Stack.pop();
  }
  cout << endl;
}
int32_t main(void) {
  fastio;
  int __ = 1;
  // cin >> __;
  while (__--) {
    accio_ac();
  }
  return 0;
}