Problem p03165 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

#define ll long long

#define ld long double

#define endl '\n'

#define vll vector<ll>

#define vvll vector<vll>

#define pll pair<ll, ll>

#define vpll vector<pll>

#define mp make_pair

#define pb push_back

#define MOD 1000000007

#define inf 1e18;

#define vfind(v, x) (find(all(v), x) != (v).end())

#define clr(c) (c).clear()

#define cres(c, n) (c).clear(), (c).resize(n)

#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define all(v) v.begin(), v.end()

#define rall(v) v.rbegin(), v.rend()

#define fst first

#define scd second

#define eb(a) emplace_back(a)

#define ef(a) emplace_front(a)

#define fr(i, n) for (ll(i) = 0; (i) < (n); ++(i))

#define fr1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))

#define frr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))

#define frab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))

#define mst(A) memset((A), 0, sizeof(A));

using namespace std;

void write(vll &oneD) {

  for (ll i = 0; i < oneD.size(); i++) {

    printf("%lld ", oneD[i]);
  }

  printf("\n");
}

void write(vvll &twoD) {

  for (ll i = 0; i < twoD.size(); i++) {

    write(twoD[i]);
  }
}

void write(vpll &oneDP) {

  fr(i, oneDP.size()) { printf("%lld %lld\n", oneDP[i].fst, oneDP[i].scd); }
  cout << "\n";
}

void write(map<ll, ll> &mpp) {

  for (map<ll, ll>::iterator it = mpp.begin(); it != mpp.end(); it++) {

    cout << it->fst << " : " << it->scd << endl;
  }

  cout << endl;
}
bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first == b.first)
    return (a.scd >= b.scd);

  return (a.first < b.first);
}

vll seive;
vll primes;

void Seive() {

  const ll maxn = 100005;

  seive.resize(maxn);
  fr(i, maxn) seive[i] = 1;

  for (ll i = 2; i * i < maxn; i++) {
    if (seive[i] == 1) {
      for (ll j = i * i; j < maxn; j += i) {
        seive[j] = 0;
      }
    }
  }
  primes.pb(2);
  for (ll i = 3; i < maxn; i += 2) {
    if (seive[i])
      primes.pb(i);
  }
}

ll isprime(ll N) {
  if (N < 2 || (!(N & 1) && N != 2))
    return 0;
  for (ll i = 3; i * i <= N; i += 2) {
    if (!(N % i))
      return 0;
  }
  return 1;
}

ll power(ll x, ll y, ll m) {

  long long int res = 1;

  x = x % m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % MOD;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}

ll modinv(ll x, ll mod = MOD) { return power(x, mod - 2, mod); }
////////////////////////////////////////////////////////////////////////////////

ll T[3005][3005];
ll B[100001];
string s1, s2;
ll lcs(ll n, ll m) {
  fr(i, n + 1) {
    fr(j, m + 1) {
      if (i == 0 || j == 0) {
        T[i][j] = 0;

      } else if (s1[i - 1] == s2[j - 1]) {
        T[i][j] = 1 + T[i - 1][j - 1];
      } else {
        T[i][j] = max(T[i - 1][j], T[i][j - 1]);
      }
    }
  }
  int res = T[n][m];
  ll p = res;
  char s[res + 1];
  // s[res] = '';
  int i = n;
  int j = m;
  string s3;

  while (j > 0 && i > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      j--;
      i--;
      res--;
      s[res] = (char)s1[i];
      s3 = s3 + ((char)s1[i]);

    } else if (T[i - 1][j] > T[i][j - 1]) {
      i--;
    } else
      j--;
  }
  for (int i = s3.length() - 1; i >= 0; i--)
    cout << s3[i];

  // cout<<endl;
  // cout<<s<<endl;
  return p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("allo.txt", "r", stdin);
#endif
  //  freopen("out.txt","w",stdout);

  ll a, b, c, d;
  ll k, l, m, n, p, q, r, t, x, y;

  // cin>>t;while(t--){    map<ll,ll> M;
  //  string s1,s2;

  cin >> s1 >> s2;
  n = s1.length();
  m = s2.length();
  p = lcs(n, m);
  // cout<<p<<endl;

  //   }
}


Fixed submission:

#include <bits/stdc++.h>

#define ll long long

#define ld long double

#define endl '\n'

#define vll vector<ll>

#define vvll vector<vll>

#define pll pair<ll, ll>

#define vpll vector<pll>

#define mp make_pair

#define pb push_back

#define MOD 1000000007

#define inf 1e18;

#define vfind(v, x) (find(all(v), x) != (v).end())

#define clr(c) (c).clear()

#define cres(c, n) (c).clear(), (c).resize(n)

#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define all(v) v.begin(), v.end()

#define rall(v) v.rbegin(), v.rend()

#define fst first

#define scd second

#define eb(a) emplace_back(a)

#define ef(a) emplace_front(a)

#define fr(i, n) for (ll(i) = 0; (i) < (n); ++(i))

#define fr1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))

#define frr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))

#define frab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))

#define mst(A) memset((A), 0, sizeof(A));

using namespace std;

void write(vll &oneD) {

  for (ll i = 0; i < oneD.size(); i++) {

    printf("%lld ", oneD[i]);
  }

  printf("\n");
}

void write(vvll &twoD) {

  for (ll i = 0; i < twoD.size(); i++) {

    write(twoD[i]);
  }
}

void write(vpll &oneDP) {

  fr(i, oneDP.size()) { printf("%lld %lld\n", oneDP[i].fst, oneDP[i].scd); }
  cout << "\n";
}

void write(map<ll, ll> &mpp) {

  for (map<ll, ll>::iterator it = mpp.begin(); it != mpp.end(); it++) {

    cout << it->fst << " : " << it->scd << endl;
  }

  cout << endl;
}
bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first == b.first)
    return (a.scd >= b.scd);

  return (a.first < b.first);
}

vll seive;
vll primes;

void Seive() {

  const ll maxn = 100005;

  seive.resize(maxn);
  fr(i, maxn) seive[i] = 1;

  for (ll i = 2; i * i < maxn; i++) {
    if (seive[i] == 1) {
      for (ll j = i * i; j < maxn; j += i) {
        seive[j] = 0;
      }
    }
  }
  primes.pb(2);
  for (ll i = 3; i < maxn; i += 2) {
    if (seive[i])
      primes.pb(i);
  }
}

ll isprime(ll N) {
  if (N < 2 || (!(N & 1) && N != 2))
    return 0;
  for (ll i = 3; i * i <= N; i += 2) {
    if (!(N % i))
      return 0;
  }
  return 1;
}

ll power(ll x, ll y, ll m) {

  long long int res = 1;

  x = x % m;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % MOD;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}

ll modinv(ll x, ll mod = MOD) { return power(x, mod - 2, mod); }
////////////////////////////////////////////////////////////////////////////////

ll T[3005][3005];
ll B[100001];
string s1, s2;
ll lcs(ll n, ll m) {
  fr(i, n + 1) {
    fr(j, m + 1) {
      if (i == 0 || j == 0) {
        T[i][j] = 0;

      } else if (s1[i - 1] == s2[j - 1]) {
        T[i][j] = 1 + T[i - 1][j - 1];
      } else {
        T[i][j] = max(T[i - 1][j], T[i][j - 1]);
      }
    }
  }
  int res = T[n][m];
  ll p = res;
  char s[res + 1];
  // s[res] = '';
  int i = n;
  int j = m;
  string s3;

  while (j > 0 && i > 0) {
    if (s1[i - 1] == s2[j - 1]) {
      j--;
      i--;
      res--;
      s[res] = (char)s1[i];
      s3 = s3 + ((char)s1[i]);

    } else if (T[i - 1][j] > T[i][j - 1]) {
      i--;
    } else
      j--;
  }
  for (int i = s3.length() - 1; i >= 0; i--)
    cout << s3[i];

  // cout<<endl;
  // cout<<s<<endl;
  return p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll a, b, c, d;
  ll k, l, m, n, p, q, r, t, x, y;

  // cin>>t;while(t--){    map<ll,ll> M;
  //  string s1,s2;

  cin >> s1 >> s2;
  n = s1.length();
  m = s2.length();
  p = lcs(n, m);
  // cout<<p<<endl;

  //   }
}