Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\  =  /0
                    ___/`---'\___
                  .' \\|     |// '.
                 / \\|||  :  |||// \
                / _||||| -:- |||||- \
               |   | \\\  -  /// |   |
               | \_|  ''\---/''  |_/ |
               \  .-\__  '-'  ___/-. /
             ___'. .'  /--.--\  `. .'___
          ."" '<  `.___\_<|>_/___.' >' "".
         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
         \  \ `_.   \_ __\ /__ _/   .-` /  /
     =====`-.____`.___ \_____/___.-`___.-'=====
                       `=---='

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>

#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define loop(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define rloop(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define loopl(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i)
#define loopr(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define count_1(n) __builtin_popcountll(n)
#define pb push_back
#define eb emplace_back
#define ab(a) (a < 0) ? (-1 * a) : a
#define mset(a, b, c) loop(i, 0, b) a[i] = c
#define F first
#define S second
#define clr(x) x.clear()
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define vi vector<int>
#define vvi vector<vi>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(p) p.begin(), p.end()
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
int flag[MX >> 6];
#define ifc(i) (flag[i >> 6] & (1 << ((i >> 1) & 31)))
#define isc(i) (flag[i >> 6] |= (1 << ((i >> 1) & 31)))
#define chk(n) (n == 2 || (n > 2 && (n & 1) && !ifc(n)))
#define Baby_Run                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
void sieve() {
  int i, j, k;
  for (i = 3; i < lmt; i += 2)
    if (!ifc(i))
      for (j = i * i, k = i << 1; j < MX; j += k)
        isc(j);
}

// const ll INF =  1e18L + 5;

int lcs[3009][3009];

inline void solve() {
  /*_Start_*/
  string s, t;
  cin >> s >> t;
  int m = s.size(), n = t.size();
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        lcs[i][j] = 0;
      else {
        if (s[i - 1] == t[j - 1])
          lcs[i][j] = lcs[i - 1][j - 1] + 1;
        else
          lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
      }
    }
  }
  string ans = "";
  int i = m, j = n;
  while (i > 0 and j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else if (lcs[i][j - 1] > lcs[i - 1][j])
      j--;
    else
      i--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  /*_End_*/
}

int main() {
  cout << fixed << setprecision(12);
  // freopen("input.txt", "r", stdin);
  Baby_Run;
  ll t = 1; //,tc=1;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*
                       _oo0oo_
                      o8888888o
                      88" . "88
                      (| -_- |)
                      0\  =  /0
                    ___/`---'\___
                  .' \\|     |// '.
                 / \\|||  :  |||// \
                / _||||| -:- |||||- \
               |   | \\\  -  /// |   |
               | \_|  ''\---/''  |_/ |
               \  .-\__  '-'  ___/-. /
             ___'. .'  /--.--\  `. .'___
          ."" '<  `.___\_<|>_/___.' >' "".
         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
         \  \ `_.   \_ __\ /__ _/   .-` /  /
     =====`-.____`.___ \_____/___.-`___.-'=====
                       `=---='

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Vikash Kumar @ Codechef/codeforces
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>

#define ll long long int
#define ll_MAX LLONG_MAX
#define ll_MIN LLONG_MIN
#define loop(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define rloop(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define loopl(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i)
#define loopr(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define count_1(n) __builtin_popcountll(n)
#define pb push_back
#define eb emplace_back
#define ab(a) (a < 0) ? (-1 * a) : a
#define mset(a, b, c) loop(i, 0, b) a[i] = c
#define F first
#define S second
#define clr(x) x.clear()
#define MOD 1000000007
#define MAX 1e9
#define MIN -1e9
#define itoc(c) ((char)(((int)'0') + c))
#define vi vector<int>
#define vvi vector<vi>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(p) p.begin(), p.end()
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
int flag[MX >> 6];
#define ifc(i) (flag[i >> 6] & (1 << ((i >> 1) & 31)))
#define isc(i) (flag[i >> 6] |= (1 << ((i >> 1) & 31)))
#define chk(n) (n == 2 || (n > 2 && (n & 1) && !ifc(n)))
#define Baby_Run                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
void sieve() {
  int i, j, k;
  for (i = 3; i < lmt; i += 2)
    if (!ifc(i))
      for (j = i * i, k = i << 1; j < MX; j += k)
        isc(j);
}

// const ll INF =  1e18L + 5;

int lcs[3009][3009];

inline void solve() {
  /*_Start_*/
  string s, t;
  cin >> s >> t;
  int m = s.size(), n = t.size();
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        lcs[i][j] = 0;
      else {
        if (s[i - 1] == t[j - 1])
          lcs[i][j] = lcs[i - 1][j - 1] + 1;
        else
          lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
      }
    }
  }
  string ans = "";
  int i = m, j = n;
  while (i > 0 and j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else if (lcs[i][j - 1] > lcs[i - 1][j])
      j--;
    else
      i--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  /*_End_*/
}

int main() {
  cout << fixed << setprecision(12);
  // freopen("input.txt", "r", stdin);
  Baby_Run;
  ll t = 1; //,tc=1;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // #endif
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}