Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
// #define hell          1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
// mp::cpp_int
#define pp pair<pii, pii>
const int hell = 1000000007;

int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int t = power(a, b / 2);
  t %= hell;
  t *= t;
  t %= hell;
  if (b % 2)
    return (t * a) % hell;
  else
    return t;
}

int gcd(int a, int b) {

  if (b == 0)
    return a;
  return gcd(b - a % b, a % b);
}

int digitsum(int x) {
  while (x > 9) {
    int ans = 0;
    while (x != 0) {
      ans += x % 10;
      x /= 10;
    }
    x = ans;
  }
  return x;
}

int pw(int a, int b) {
  int x = a;
  rep(i, 1, b) { a = a * x; }
  return a;
}

int lcs[3001][3001];

string a, b;
int LCS(int n, int m) {
  if (n == 0 || m == 0)
    return 0;
  if (lcs[n][m])
    return lcs[n][m];
  int t;
  if (a[n - 1] == b[m - 1]) {

    t = LCS(n - 1, m - 1) + 1;

  } else
    t = max(LCS(n - 1, m), LCS(n, m - 1));
  lcs[n][m] = t;
  return t;
}

int solve() {

  cin >> a >> b;
  int n = a.size();
  int m = b.size();
  memset(lcs, 0, sizeof(lcs));

  LCS(n, m);

  int i = n, j = m;
  string ans = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1]; // cout<<a[i-1]<<endl;
      i--;
      j--;
      continue;
    }
    if (lcs[i - 1][j] > lcs[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(all(ans));
  cout << ans;

  return 0;
}

signed main() {
  int t = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;

  int k = 1;
  while (t--) {
    // cout<<"case #"<<k<<":"<<" ";k++;
    solve();
  }

  time_req = clock() - time_req;
  // cout <<endl<< "Processor time taken is "
  //<< (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
// #define hell          1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
// mp::cpp_int
#define pp pair<pii, pii>
const int hell = 1000000007;

int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int t = power(a, b / 2);
  t %= hell;
  t *= t;
  t %= hell;
  if (b % 2)
    return (t * a) % hell;
  else
    return t;
}

int gcd(int a, int b) {

  if (b == 0)
    return a;
  return gcd(b - a % b, a % b);
}

int digitsum(int x) {
  while (x > 9) {
    int ans = 0;
    while (x != 0) {
      ans += x % 10;
      x /= 10;
    }
    x = ans;
  }
  return x;
}

int pw(int a, int b) {
  int x = a;
  rep(i, 1, b) { a = a * x; }
  return a;
}

int lcs[3001][3001];

string a, b;
int LCS(int n, int m) {
  if (n == 0 || m == 0)
    return 0;
  if (lcs[n][m])
    return lcs[n][m];
  int t;
  if (a[n - 1] == b[m - 1]) {

    t = LCS(n - 1, m - 1) + 1;

  } else
    t = max(LCS(n - 1, m), LCS(n, m - 1));
  lcs[n][m] = t;
  return t;
}

int solve() {

  cin >> a >> b;
  int n = a.size();
  int m = b.size();
  memset(lcs, 0, sizeof(lcs));

  lcs[0][0] = 0;
  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= m; j++) {
      if (a[i - 1] == b[j - 1]) {
        lcs[i][j] = 1 + lcs[i - 1][j - 1];
      } else {
        lcs[i][j] = max(lcs[i][j - 1], lcs[i - 1][j]);
      }
    }
  }

  int i = n, j = m;
  string ans = "";
  while (i > 0 && j > 0) {
    if (a[i - 1] == b[j - 1]) {
      ans += a[i - 1]; // cout<<a[i-1]<<endl;
      i--;
      j--;
      continue;
    }
    if (lcs[i - 1][j] > lcs[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(all(ans));
  cout << ans;

  return 0;
}

signed main() {
  int t = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);

  clock_t time_req;

  // Without using pow function
  time_req = clock();

  // cin>>t;

  int k = 1;
  while (t--) {
    // cout<<"case #"<<k<<":"<<" ";k++;
    solve();
  }

  time_req = clock() - time_req;
  // cout <<endl<< "Processor time taken is "
  //<< (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}