Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
/******************************************
 * AUTHOR : RAJAGOPALAN *
 * NICK : ARNO *
 * INSTITUTION : VIT *
 ******************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define alli(a, n, k) (a + k), (a + n + k)
#define REP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k)
#define REPI(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k)
#define REPITER(it, a)                                                         \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define eps 1e-6
#define pi 3.141592653589793
using namespace std;
template <class T> inline T gcd(T x, T y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
typedef vector<pair<int, PII>> VPPI;
const int MOD = 1e9 + 7;
const int INF = 1e9;
string LCS(string s, string t, int i, int j, string res) {
  if (i >= s.size() || j >= t.size())
    return res;

  string a1, a2, a3;
  if (s[i] == t[j]) {
    a1 = LCS(s, t, i + 1, j + 1, res + s[i]);
  }

  else {
    a2 = LCS(s, t, i + 1, j, res);
    a3 = LCS(s, t, i, j + 1, res);
  }
  if (a1.size() >= a2.size() && a1.size() >= a3.size())
    return a1;
  else if (a2.size() >= a1.size() && a2.size() >= a3.size())
    return a2;
  else
    return a3;
}
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  cout << LCS(s, t, 0, 0, "");
  return 0;
}

Fixed submission:
/******************************************
 * AUTHOR : RAJAGOPALAN *
 * NICK : ARNO *
 * INSTITUTION : VIT *
 ******************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define alli(a, n, k) (a + k), (a + n + k)
#define REP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k)
#define REPI(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k)
#define REPITER(it, a)                                                         \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define eps 1e-6
#define pi 3.141592653589793
using namespace std;
template <class T> inline T gcd(T x, T y) {
  if (!y)
    return x;
  return gcd(y, x % y);
}
typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
typedef vector<pair<int, PII>> VPPI;
const int MOD = 1e9 + 7;
const int INF = 1e9;
string LCS(string s, string t, int i, int j, string res) {
  if (i >= s.size() || j >= t.size())
    return res;

  string a1, a2, a3;
  if (s[i] == t[j]) {
    a1 = LCS(s, t, i + 1, j + 1, res + s[i]);
  }

  else {
    a2 = LCS(s, t, i + 1, j, res);
    a3 = LCS(s, t, i, j + 1, res);
  }
  if (a1.size() >= a2.size() && a1.size() >= a3.size())
    return a1;
  else if (a2.size() >= a1.size() && a2.size() >= a3.size())
    return a2;
  else
    return a3;
}
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  int u = s.size(), v = t.size();
  int dp[u + 1][v + 1];
  for (int i = 0; i <= u; ++i) {
    for (int j = 0; j <= v; ++j) {
      if (i == 0 || 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]);
    }
  }
  string ans = "";
  int i = u, j = v;
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}