Problem p00451 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int memo[4040][4040] = {};
int main(void) {
  string s, t;
  while (cin >> s && s.size()) {
    int res = 0;
    cin >> t;
    memset(memo, 0, sizeof memo);
    REP(i, s.size()) {
      REP(j, t.size()) {
        if (memo[i][j] != 0)
          continue;
        int cnt = 0;
        if (s[i] == t[j]) {
          cnt++;
          memo[i][j] = cnt;
          while (s[i + cnt] == t[j + cnt] && i + cnt < s.size() &&
                 j + cnt < t.size()) {
            cnt++;
            memo[i + cnt][j + cnt] = cnt;
          }
          res = max(res, cnt);
        }
      }
    }
    cout << res << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  string s, t;
  while (cin >> s && s.size()) {
    int memo[4040][4040] = {};
    int res = 0;
    cin >> t;
    REP(i, s.size()) {
      REP(j, t.size()) {
        if (memo[i][j] != 0)
          continue;
        int cnt = 0;
        if (s[i] == t[j]) {
          cnt++;
          memo[i][j] = cnt;
          while (s[i + cnt] == t[j + cnt] && i + cnt < s.size() &&
                 j + cnt < t.size()) {
            cnt++;
            memo[i + cnt][j + cnt] = cnt;
          }
          res = max(res, cnt);
        }
      }
    }
    cout << res << endl;
  }
}