Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// debug
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl;
#define ddd(x, y, z)                                                           \
  cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")"       \
       << endl;
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
#define damp(m)                                                                \
  for (auto macro_pair : m) {                                                  \
    cerr << macro_pair.first << ":" << macro_pair.second << endl;              \
  }
#define dpair(p)                                                               \
  cerr << #p ":"                                                               \
       << "(" << p.first << "," << p.second << ")" << endl;
// iterate
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(v) v.begin(), v.end()
// etc
#define chmin(mi, val) mi = min(mi, val);
#define chmax(ma, val) ma = max(ma, val);
using ll = long long;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;

  vector<vector<int>> dp(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == 0 or j == 0) {
        if (s[i] == s[j]) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 0;
        }
        continue;
      }
      if (s[i] == s[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  d(s) ddump(dp) int ma = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= j)
        continue;
      int now = min(dp[i][j], j - i);
      chmax(ma, now);
    }
  }
  cout << ma << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// debug
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl;
#define ddd(x, y, z)                                                           \
  cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")"       \
       << endl;
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
#define damp(m)                                                                \
  for (auto macro_pair : m) {                                                  \
    cerr << macro_pair.first << ":" << macro_pair.second << endl;              \
  }
#define dpair(p)                                                               \
  cerr << #p ":"                                                               \
       << "(" << p.first << "," << p.second << ")" << endl;
// iterate
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define reprev(i, n) reprrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(v) v.begin(), v.end()
// etc
#define chmin(mi, val) mi = min(mi, val);
#define chmax(ma, val) ma = max(ma, val);
using ll = long long;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;

  vector<vector<int>> dp(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == 0 or j == 0) {
        if (s[i] == s[j]) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 0;
        }
        continue;
      }
      if (s[i] == s[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= j)
        continue;
      int now = min(dp[i][j], j - i);
      chmax(ma, now);
    }
  }
  cout << ma << endl;

  return 0;
}