Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define quick                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)

void solve() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length(), m = t.length();
  vector<vector<int>> v(n, vector<int>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      v[i][j] = (s[i] == t[j]);
      if (i and j) {
        v[i][j] = (s[i] == t[j]) + v[i - 1][j - 1];
      }
      if (i) {
        v[i][j] = max(v[i][j], v[i - 1][j]);
      }
      if (j) {
        v[i][j] = max(v[i][j], v[i][j - 1]);
      }
    }
  }
  int in = n - 1, jn = m - 1;
  bool pr = true;
  string ans = "";
  // for (auto i : v) {
  //     for (auto j : i) {
  //         cout << j << " ";
  //     }
  //     cout << '\n';
  // }
  while (in >= 0 and jn >= 0) {
    // cout << in << " "/ << jn << '\n';
    if (jn) {
      if (v[in][jn] == v[in][jn - 1]) {
        jn--;
        continue;
      }
    }
    if (in) {
      if (v[in][jn] == v[in - 1][jn]) {
        in--;
        continue;
      }
    }
    if (s[in] == t[jn]) {
      ans += s[in];
      in--;
      jn--;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
}

int main() {
  quick;
  solve();
  // ll t;
  // cin >> t;
  // while (t--) {
  //     solve();
  // }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define quick                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)

void solve() {
  string s, t;
  cin >> s;
  cin >> t;
  int n = s.length(), m = t.length();
  vector<vector<int>> v(n, vector<int>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      v[i][j] = (s[i] == t[j]);
      if (i and j) {
        v[i][j] = (s[i] == t[j]) + v[i - 1][j - 1];
      }
      if (i) {
        v[i][j] = max(v[i][j], v[i - 1][j]);
      }
      if (j) {
        v[i][j] = max(v[i][j], v[i][j - 1]);
      }
    }
  }
  int in = n - 1, jn = m - 1;
  bool pr = true;
  string ans = "";
  // for (auto i : v) {
  //     for (auto j : i) {
  //         cout << j << " ";
  //     }
  //     cout << '\n';
  // }
  while (in >= 0 and jn >= 0) {
    // cout << in << " "/ << jn << '\n';
    if (jn) {
      if (v[in][jn] == v[in][jn - 1]) {
        jn--;
        continue;
      }
    }
    if (in) {
      if (v[in][jn] == v[in - 1][jn]) {
        in--;
        continue;
      }
    }
    if (s[in] == t[jn]) {
      ans += s[in];
      in--;
      jn--;
    } else {
      break;
    }
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
}

int main() {
  quick;
  solve();
  // ll t;
  // cin >> t;
  // while (t--) {
  //     solve();
  // }
}