Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple

#define N 311111
#define A 1111111
#define BLOCK 555 // ~sqrt(N)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int INF = 1e9 + 5;

ll bin_search(ll arr[], ll x, ll l, ll r) {
  while (l < r) {
    ll mid = (l + r + 1) / 2;
    ll power = (pow(2, mid + 1) * x) - (arr[mid]);
    // cout<<l<<" "<<r<<" "<<mid<<" "<<power<<endl;
    if (power > 0) {
      l = mid;
    } else if (power <= 0) {
      r = mid - 1;
    }
  }
  return l;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n, q;
    string a, b;
    string ans = "";
    cin >> a >> b;
    int dp[a.length() + 1][b.length() + 1];
    for (int i = 0; i <= a.length(); i++) {
      dp[i][0] = 0;
    }
    for (int j = 0; j <= b.length(); j++) {
      dp[0][j] = 0;
    }
    for (int i = 1; i <= a.length(); i++) {
      for (int j = 1; j <= b.length(); j++) {
        if (a[i - 1] == b[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    int i = a.length();
    int j = b.length();
    while (i >= 1 || j >= 1) {
      if (a[i - 1] == b[j - 1]) {
        ans = a[i - 1] + ans;
        i--;
        j--;
      } else {
        if (dp[i - 1][j] == dp[i][j]) {
          i--;
        } else {
          j--;
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple

#define N 311111
#define A 1111111
#define BLOCK 555 // ~sqrt(N)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int INF = 1e9 + 5;

ll bin_search(ll arr[], ll x, ll l, ll r) {
  while (l < r) {
    ll mid = (l + r + 1) / 2;
    ll power = (pow(2, mid + 1) * x) - (arr[mid]);
    // cout<<l<<" "<<r<<" "<<mid<<" "<<power<<endl;
    if (power > 0) {
      l = mid;
    } else if (power <= 0) {
      r = mid - 1;
    }
  }
  return l;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n, q;
    string a, b;
    string ans = "";
    cin >> a >> b;
    int dp[a.length() + 1][b.length() + 1];
    for (int i = 0; i <= a.length(); i++) {
      dp[i][0] = 0;
    }
    for (int j = 0; j <= b.length(); j++) {
      dp[0][j] = 0;
    }
    for (int i = 1; i <= a.length(); i++) {
      for (int j = 1; j <= b.length(); j++) {
        if (a[i - 1] == b[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    int i = a.length();
    int j = b.length();
    while (i >= 1 && j >= 1) {
      if (a[i - 1] == b[j - 1]) {
        ans = a[i - 1] + ans;
        i--;
        j--;
      } else {
        if (dp[i - 1][j] == dp[i][j]) {
          i--;
        } else {
          j--;
        }
      }
    }
    cout << ans << endl;
  }
}