Problem p03165 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define _ % MOD
#define __ %= MOD

#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))

#define swap(a, b)                                                             \
  {                                                                            \
    a = a + b;                                                                 \
    b = a - b;                                                                 \
    a = a - b;                                                                 \
  }
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define fbo find_by_order
#define ook order_of_key

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
} // modular exponent

int main() {
  ios_base::sync_with_stdio(false); // don't use printf and scanf
  cin.tie(NULL);                    // cout<<fixed<<setprecision
  string s, t;
  vector<char> str;
  getline(cin, s);
  getline(cin, t);
  int rows = s.length() + 1;
  int cols = t.length() + 1;
  int D[rows][cols];
  int i, j;
  for (i = 0; i < cols; i++)
    D[0][i] = 0;
  for (i = 0; i < rows; i++)
    D[i][0] = 0;
  for (i = 1; i < rows; i++) {
    // int a=0;
    for (j = 1; j < cols; j++) {
      //	cout<<"i = "<<i<<" j = "<<j<<" s["<<i-1<<"] = "<<s[i-1]<<"
      //t["<<j-1<<"] = "<<t[j-1]<<"\n";
      if (s[i - 1] == t[j - 1]) {
        // a++;
        //	if(find(str.begin(),str.end(),s[i])==str.end())
        // if(a==1)str.pb(s[i-1]);
        D[i][j] = D[i - 1][j - 1] + 1;
      } else {
        D[i][j] = max(D[i - 1][j], D[i][j - 1]);
      }
    }
  }
  i = rows - 1;
  j = cols - 1;
  while (i || j) {
    if (s[i - 1] == t[j - 1]) {
      str.pb(s[i - 1]);
      i--;
      j--;
    } else if (D[i - 1][j] >= D[i][j - 1])
      i--;
    else
      j--;
  }
  //	cout<<s[i-1]<<" chara "<<t[j-1]<<"\n";
  //	cout<<"size of lcs = "<<D[rows-1][cols-1]<<"\n";
  reverse(str.begin(), str.end());
  for (i = 0; i < str.size(); i++)
    cout << str[i];
  cout << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define _ % MOD
#define __ %= MOD

#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))

#define swap(a, b)                                                             \
  {                                                                            \
    a = a + b;                                                                 \
    b = a - b;                                                                 \
    a = a - b;                                                                 \
  }
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define fbo find_by_order
#define ook order_of_key

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
} // modular exponent

int main() {
  ios_base::sync_with_stdio(false); // don't use printf and scanf
  cin.tie(NULL);                    // cout<<fixed<<setprecision
  string s, t;
  vector<char> str;
  getline(cin, s);
  getline(cin, t);
  int rows = s.length() + 1;
  int cols = t.length() + 1;
  int D[rows][cols];
  int i, j;
  for (i = 0; i < cols; i++)
    D[0][i] = 0;
  for (i = 0; i < rows; i++)
    D[i][0] = 0;
  for (i = 1; i < rows; i++) {
    // int a=0;
    for (j = 1; j < cols; j++) {
      //	cout<<"i = "<<i<<" j = "<<j<<" s["<<i-1<<"] = "<<s[i-1]<<"
      //t["<<j-1<<"] = "<<t[j-1]<<"\n";
      if (s[i - 1] == t[j - 1]) {
        // a++;
        //	if(find(str.begin(),str.end(),s[i])==str.end())
        // if(a==1)str.pb(s[i-1]);
        D[i][j] = D[i - 1][j - 1] + 1;
      } else {
        D[i][j] = max(D[i - 1][j], D[i][j - 1]);
      }
    }
  }
  i = rows - 1;
  j = cols - 1;
  while (i && j) {
    if (s[i - 1] == t[j - 1]) {
      str.pb(s[i - 1]);
      i--;
      j--;
    } else if (D[i - 1][j] >= D[i][j - 1])
      i--;
    else
      j--;
  }
  //	cout<<s[i-1]<<" chara "<<t[j-1]<<"\n";
  //	cout<<"size of lcs = "<<D[rows-1][cols-1]<<"\n";
  reverse(str.begin(), str.end());
  for (i = 0; i < str.size(); i++)
    cout << str[i];
  cout << "\n";
}