Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[1005][1005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string a, b, t = "";
  cin >> a >> b;
  for (int i = 1; i <= a.size(); i++)
    for (int j = 1; j <= b.size(); 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.size(), j = b.size();
  while (dp[i][j]) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] > dp[i - 1][j - 1])
      i--, j--, t += a[i];
  }
  reverse(t.begin(), t.end());
  cout << t;
}
/*#include <stack>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using LL = long long;
#define eps 1e-8
#define fi first
#define se second
#define eb emplace_back
#define close
std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) #define
FOR(i, x, y) for (LL i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y)
for (LL i = (x), _##i = (y); i > _##i; --i) #define SORT_UNIQUE(c)
(sort(c.begin(),c.end()),
c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define CASET int ___T;
scanf("%d", &___T); for(int __CS=1;__CS<=___T;__CS++) typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937 dlsrand(random_device{}());
mt19937 mrand(std::chrono::system_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x;}
LL bin(LL x, LL n, LL MOD) {LL ret = MOD != 1;for (x %= MOD; n; n >>= 1, x = x *
x % MOD)if (n & 1) ret = ret * x % MOD;return ret;} inline LL get_inv(LL x, LL
p) { return bin(x, p - 2, p); } const double PI = acos(-1.0); constexpr int maxn
= 1e6+10; constexpr int INF = 0x3f3f3f3f; constexpr ll linf =
0x3f3f3f3f3f3f3f3f; constexpr int mod = 1e9+7; int dp[1005][1005]; int main()
{
    string s, t;
    cin >> s >> t;
    int len1 = s.size(), len2= t.size();
    for(int i = 0; i < len1 ; i++){
        for(int j = 0; j < len2; j++){
            if(s[i] == t[i]){
                dp[i+1][j+1] = dp[i][j] + 1;
            }else {
                dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
            }
        }
    }cout << dp[len1 ] [len2] ;
}*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[3005][3005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string a, b, t = "";
  cin >> a >> b;
  for (int i = 1; i <= a.size(); i++)
    for (int j = 1; j <= b.size(); 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.size(), j = b.size();
  while (dp[i][j]) {
    if (dp[i][j] == dp[i - 1][j])
      i--;
    else if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] > dp[i - 1][j - 1])
      i--, j--, t += a[i];
  }
  reverse(t.begin(), t.end());
  cout << t;
}
/*#include <stack>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using LL = long long;
#define eps 1e-8
#define fi first
#define se second
#define eb emplace_back
#define close
std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) #define
FOR(i, x, y) for (LL i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y)
for (LL i = (x), _##i = (y); i > _##i; --i) #define SORT_UNIQUE(c)
(sort(c.begin(),c.end()),
c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define CASET int ___T;
scanf("%d", &___T); for(int __CS=1;__CS<=___T;__CS++) typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937 dlsrand(random_device{}());
mt19937 mrand(std::chrono::system_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x;}
LL bin(LL x, LL n, LL MOD) {LL ret = MOD != 1;for (x %= MOD; n; n >>= 1, x = x *
x % MOD)if (n & 1) ret = ret * x % MOD;return ret;} inline LL get_inv(LL x, LL
p) { return bin(x, p - 2, p); } const double PI = acos(-1.0); constexpr int maxn
= 1e6+10; constexpr int INF = 0x3f3f3f3f; constexpr ll linf =
0x3f3f3f3f3f3f3f3f; constexpr int mod = 1e9+7; int dp[1005][1005]; int main()
{
    string s, t;
    cin >> s >> t;
    int len1 = s.size(), len2= t.size();
    for(int i = 0; i < len1 ; i++){
        for(int j = 0; j < len2; j++){
            if(s[i] == t[i]){
                dp[i+1][j+1] = dp[i][j] + 1;
            }else {
                dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
            }
        }
    }cout << dp[len1 ] [len2] ;
}*/