Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ii int64_t
#define PI 3.14
#define lli long long int
#define li long int
#define deb(x) cout << "\nhi" << x << "\n"
#define mod 1000000007
#define f(x, n) for (int x = 0; x < n; ++x)
#define ff(x, y, z) for (x = y; x <= z; ++x)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// while(scanf("%d",&n)!=EOF)

using namespace std;

int main()

{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  string s, t, ans;
  int64_t i, j, x, y;
  int dp[304][304];
  memset(dp, 0, sizeof(dp));
  cin >> s >> t;

  for (i = 1; i <= s.length(); i++)
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }

  i = s.length();
  j = t.length();
  while (true) {
    if (i > 0 && dp[i][j] == dp[i - 1][j])
      i--;
    else if (j > 0 && dp[i][j] == dp[i][j - 1])
      j--;
    else if (i > 0 && j > 0 && s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    }

    else
      break;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ii int64_t
#define PI 3.14
#define lli long long int
#define li long int
#define deb(x) cout << "\nhi" << x << "\n"
#define mod 1000000007
#define f(x, n) for (int x = 0; x < n; ++x)
#define ff(x, y, z) for (x = y; x <= z; ++x)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// while(scanf("%d",&n)!=EOF)

using namespace std;

int main()

{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  string s, t, ans;
  int64_t i, j, x, y;
  int dp[3004][3004];
  memset(dp, 0, sizeof(dp));
  cin >> s >> t;

  for (i = 1; i <= s.length(); i++)
    for (j = 1; j <= t.length(); j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }

  i = s.length();
  j = t.length();
  while (true) {
    if (i > 0 && dp[i][j] == dp[i - 1][j])
      i--;
    else if (j > 0 && dp[i][j] == dp[i][j - 1])
      j--;
    else if (i > 0 && j > 0 && s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    }

    else
      break;
  }
  reverse(ans.begin(), ans.end());
  cout << ans;
}