Problem p02913 (C++)

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

#define TYPE(c) remove_reference_t<decltype(c)>
#define REP(i, n) for (TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for (TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c) c.begin(), c.end()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD = (int)1e9 + 7;
constexpr int INF = (int)1e9 + 1;
constexpr ll LINF = (ll)1e18 + 1;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T>
auto operator<<(ostream &os, const T &t) ->
    typename conditional<true, ostream &, decltype(*t.begin())>::type {
  os << "[";
  FOR(v, t) {
    if (v != t.begin())
      os << ",";
    os << *v;
  }
  os << "]";
  return os;
}
ostream &operator<<(ostream &os, const string &s) {
  FOR(v, s) os << *v;
  return os;
}

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

  int N;
  std::cin >> N;
  string S;
  std::cin >> S;
  string S_ori = S;
  S = string("$", N) + S + string("$", N);

  int max_num = 0;
  REP(i, S.size() - N) {
    int num = 0;
    REP(j, S_ori.size()) {
      if (S_ori[j] == S[i + j]) {
        int tmp = 1;
        while (j < S_ori.size()) {
          j++;
          if (S_ori[j] == S[i + j])
            tmp++;
          else
            break;
        }
        chmax(num, tmp);
        if (abs(N - (int)i) < num) {
          num = abs(N - (int)i);
          break;
        }
      }
    }
    chmax(max_num, num);
  }

  std::cout << max_num << std::endl;

  return 0;
}


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

#define TYPE(c) remove_reference_t<decltype(c)>
#define REP(i, n) for (TYPE(n) i = 0; i < n; i++)
#define FOR(v, c) for (TYPE(c.begin()) v = c.begin(); v != c.end(); v++)
#define ALL(c) c.begin(), c.end()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<TYPE(c)::value_type>())
#define UNIQUE(c) c.erase(unique(ALL(c)), c.end())

using namespace std;
using ll = long long;

constexpr int MOD = (int)1e9 + 7;
constexpr int INF = (int)1e9 + 1;
constexpr ll LINF = (ll)1e18 + 1;
template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T>
auto operator<<(ostream &os, const T &t) ->
    typename conditional<true, ostream &, decltype(*t.begin())>::type {
  os << "[";
  FOR(v, t) {
    if (v != t.begin())
      os << ",";
    os << *v;
  }
  os << "]";
  return os;
}
ostream &operator<<(ostream &os, const string &s) {
  FOR(v, s) os << *v;
  return os;
}

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

  int N;
  std::cin >> N;
  string S;
  std::cin >> S;
  string S_ori = S;
  S = string(N, '$') + S + string(N, '$');

  int max_num = 0;
  REP(i, S.size() - N) {
    int num = 0;
    REP(j, S_ori.size()) {
      if (S_ori[j] == S[i + j]) {
        int tmp = 1;
        while (j < S_ori.size()) {
          j++;
          if (S_ori[j] == S[i + j])
            tmp++;
          else
            break;
        }
        chmax(num, tmp);
        if (abs(N - (int)i) < num) {
          num = abs(N - (int)i);
          break;
        }
      }
    }
    chmax(max_num, num);
  }

  std::cout << max_num << std::endl;

  return 0;
}