Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long int
#define pb push_back
#define fi(n) for (int i = 0; i < n; i++)
#define fi1(n) for (int i = 1; i < n; i++)
#define fj(n) for (int j = 0; j < n; j++)
#define fj1(n) for (int j = 1; j < n; j++)
#define rep(z, a, n) for (z = a; z < n; z++)
#define rep2(z, a, n) for (z = a; z < n; z += 2)
#define clr(x) memset(x, 0, sizeof(x))
#define print(x, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << x[i] << " ";                                                       \
  cout << "\n"
#define nl "\n"
#define vi vector<int>
#define mp make_pair
#define pairi pair<int, int>
#define vpairi vector<pair<int, int>>
#define ff first
#define ss second
#define inf 2e18
#define input                                                                  \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//input.txt"
#define output                                                                 \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//output.txt"
#define error                                                                  \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//error.txt"

int ctoi(char a) {
  int x = a - 48;
  return x;
}
string itos(int a) {
  string out_string;
  stringstream ss;
  ss << a;
  out_string = ss.str();
  return out_string;
}
char itoc(int a) { return itos(a)[0]; }
int pow(int e, int x) {
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans *= e;
    e *= e;
    x >>= 1;
  }
  return ans;
}
string bin(int x) {
  bitset<sizeof(1) * CHAR_BIT> bits(x);
  string b = bits.to_string();
  return b;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen(input, "r", stdin);
  freopen(output, "w", stdout);
  freopen(error, "w", stderr);
#endif
  int T = 1;
  // cin>>T;
  while (T--) {
    int n, k, m, x, y;
    // cin>>n;
    // int a[n];clr(a);
    string a, b;
    cin >> a >> b;
    n = a.length();
    m = b.length();
    int ans[n + 1][m + 1];
    clr(ans);
    fi1(n + 1) {
      fj1(m + 1) {
        if (a[i - 1] == b[j - 1]) {
          ans[i][j] = ans[i - 1][j - 1] + 1;
        } else {
          ans[i][j] = max(ans[i - 1][j], ans[i][j - 1]);
        }
      }
    }
    // cout<<ans[n][m]<<nl;
    list<char> lcs;
    int i = n, j = m;
    while (i >= 1 && j >= 1) {
      if (a[i - 1] == b[j - 1]) {
        lcs.push_front(a[i - 1]);
        i--;
        j--;
      } else {
        if (ans[i - 1][j] > ans[i][j - 1])
          i--;
        else
          j--;
      }
    }
    for (auto x : lcs)
      cout << x;
    cout << nl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long int
#define pb push_back
#define fi(n) for (int i = 0; i < n; i++)
#define fi1(n) for (int i = 1; i < n; i++)
#define fj(n) for (int j = 0; j < n; j++)
#define fj1(n) for (int j = 1; j < n; j++)
#define rep(z, a, n) for (z = a; z < n; z++)
#define rep2(z, a, n) for (z = a; z < n; z += 2)
#define clr(x) memset(x, 0, sizeof(x))
#define print(x, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << x[i] << " ";                                                       \
  cout << "\n"
#define nl "\n"
#define vi vector<int>
#define mp make_pair
#define pairi pair<int, int>
#define vpairi vector<pair<int, int>>
#define ff first
#define ss second
#define inf 2e18
#define input                                                                  \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//input.txt"
#define output                                                                 \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//output.txt"
#define error                                                                  \
  "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//error.txt"

int ctoi(char a) {
  int x = a - 48;
  return x;
}
string itos(int a) {
  string out_string;
  stringstream ss;
  ss << a;
  out_string = ss.str();
  return out_string;
}
char itoc(int a) { return itos(a)[0]; }
int pow(int e, int x) {
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans *= e;
    e *= e;
    x >>= 1;
  }
  return ans;
}
string bin(int x) {
  bitset<sizeof(1) * CHAR_BIT> bits(x);
  string b = bits.to_string();
  return b;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
#endif
  int T = 1;
  // cin>>T;
  while (T--) {
    int n, k, m, x, y;
    // cin>>n;
    // int a[n];clr(a);
    string a, b;
    cin >> a >> b;
    n = a.length();
    m = b.length();
    int ans[n + 1][m + 1];
    clr(ans);
    fi1(n + 1) {
      fj1(m + 1) {
        if (a[i - 1] == b[j - 1]) {
          ans[i][j] = ans[i - 1][j - 1] + 1;
        } else {
          ans[i][j] = max(ans[i - 1][j], ans[i][j - 1]);
        }
      }
    }
    // cout<<ans[n][m]<<nl;
    list<char> lcs;
    int i = n, j = m;
    while (i >= 1 && j >= 1) {
      if (a[i - 1] == b[j - 1]) {
        lcs.push_front(a[i - 1]);
        i--;
        j--;
      } else {
        if (ans[i - 1][j] > ans[i][j - 1])
          i--;
        else
          j--;
      }
    }
    for (auto x : lcs)
      cout << x;
    cout << nl;
  }
  return 0;
}