prithivMLmods/Coder-Stat huggingface dataset:
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int table[5001][5001];
int main() {
  int N;
  cin >> N;
  char S[5001];
  cin >> S;

  for (int diff = 1; diff < N; diff++) {
    for (int i2 = N - 1; i2 >= 0; i2--) {
      int i1 = i2 - diff;
      if (i2 < 0)
        break;
      if (S[i1] == S[i2]) {
        if (i2 == N - 1) {
          table[i1][i2] = 1;
        } else {
          table[i1][i2] = min(i2 - i1, 1 + table[i1 + 1][i2 + 1]);
        }
      }
    }
  }

  int ans = 0;
  int a1 = -1;
  int a2 = -1;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (ans < table[i][j]) {
        ans = table[i][j];
        a1 = i;
        a2 = j;
      }
    }
  }
  cout << ans << endl;
  // cerr << a1 << " " << a2 << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int table[5001][5001];
int main() {
  int N;
  cin >> N;
  char S[5001];
  cin >> S;

  for (int diff = 1; diff < N; diff++) {
    for (int i2 = N - 1; i2 >= 0; i2--) {
      int i1 = i2 - diff;
      if (i1 < 0)
        break;
      if (S[i1] == S[i2]) {
        if (i2 == N - 1) {
          table[i1][i2] = 1;
        } else {
          table[i1][i2] = min(i2 - i1, 1 + table[i1 + 1][i2 + 1]);
        }
      }
    }
  }

  int ans = 0;
  int a1 = -1;
  int a2 = -1;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (ans < table[i][j]) {
        ans = table[i][j];
        a1 = i;
        a2 = j;
      }
    }
  }
  cout << ans << endl;
  // cerr << a1 << " " << a2 << endl;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

ll dp[2502][5001];

void solve(long long N, string S) {
  ll res = 0;
  ll s[N];
  rep(i, N) s[i] = S[i] - 'a' + 1;
  repn(i, N / 2 + 1) {
    map<ll, ll> m;
    repn(j, N) {
      if (j + i - 2 >= N)
        break;
      dp[i][j] = (dp[i - 1][j] * 27 + (S[i + j - 2] - 'a' + 1)) % 90071992547;
      if (res != i) {
        ll n = m[dp[i][j]];
        if (n > 0) {
          if (j > n + i - 1) {
            res = i;
          }
        } else {
          m[dp[i][j]] = j;
        }
      }
    }
  }
  cout << res << endl;
}

int main() {
  string S;
  long long N;
  scanf("%lld", &N);
  cin >> S;
  solve(N, S);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

ll dp[2502][5001];

void solve(long long N, string S) {
  ll res = 0;
  ll s[N];
  rep(i, N) s[i] = S[i] - 'a' + 1;
  repn(i, N / 2 + 1) {
    map<ll, ll> m;
    repn(j, N) {
      if (j + i - 2 >= N)
        break;
      dp[i][j] = (dp[i - 1][j] * 27 + (S[i + j - 2] - 'a' + 1)) % 90071992547;
      if (res != i) {
        ll n = m[dp[i][j]];
        if (n > 0) {
          if (j > n + i - 1) {
            res = i;
          }
        } else {
          m[dp[i][j]] = j;
        }
      }
    }
    if (res != i)
      break;
  }
  cout << res << endl;
}

int main() {
  string S;
  long long N;
  scanf("%lld", &N);
  cin >> S;
  solve(N, S);
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <array>
#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;

  std::string S;
  std::cin >> S;

  // 各アルファベットの登場箇所
  std::array<std::vector<short>, 26 * 26> top;
  for (int i = 0; i < (N - 1); ++i) {
    top[(((size_t)S[i] - 'a') * 26) + (size_t)S[(size_t)i + 1] - 'a'].push_back(
        (short)i);
  }

  size_t max = 0;

  for (auto &alpha : top) {
    const int size = (int)alpha.size();
    for (int i = 0; i < (size - 1); ++i) {
      for (int j = i + 1; j < size; ++j) {
        const size_t maxLength =
            std::min(std::min(alpha[j] - alpha[i], (N + 1) / 2), N - alpha[j]);
        // 先頭2文字は合致しているので2スタート
        size_t length;
        for (length = 2; length < maxLength; ++length) {
          // 一致文字数を調べる
          if (S[alpha[i] + length] != S[alpha[j] + length]) {
            // 違う文字が見つかった
            break;
          }
        }
        if (max < length) {
          max = length;
        }
      }
    }
  }

  if (max <= 1) {
    max = 0;
  }

  std::cout << max;
  return 0;
}


Fixed submission:
#include <array>
#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;

  std::string S;
  std::cin >> S;

  // 各アルファベットの登場箇所
  std::array<std::vector<short>, 26 * 26> top;
  for (int i = 0; i < (N - 1); ++i) {
    top[(((size_t)S[i] - 'a') * 26) + (size_t)S[(size_t)i + 1] - 'a'].push_back(
        (short)i);
  }

  size_t max = 0;

  for (auto &alpha : top) {
    const int size = (int)alpha.size();
    for (int i = 0; i < (size - 1); ++i) {
      for (int j = i + 1; j < size; ++j) {
        const size_t maxLength =
            std::min(std::min(alpha[j] - alpha[i], (N + 1) / 2), N - alpha[j]);
        if (maxLength < max) {
          // すでに短い
          continue;
        }
        // 先頭2文字は合致しているので2スタート
        size_t length;
        for (length = 2; length < maxLength; ++length) {
          // 一致文字数を調べる
          if (S[alpha[i] + length] != S[alpha[j] + length]) {
            // 違う文字が見つかった
            break;
          }
        }
        if (max < length) {
          max = length;
        }
      }
    }
  }

  if (max <= 1) {
    max = 0;
  }

  std::cout << max;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll n;
string s;
bool hogege = true;
bool check(ll mid) {
  map<string, pair<ll, ll>> MP;
  for (ll i = 0; i <= s.size() - mid; i++) {
    string ss = SUBS(s, i + 1, i + mid);
    ll num = MP[ss].F, num2 = MP[ss].S;
    if (num == 0) {
      MP[ss] = mp(i + 1, i + 1);
    } else {
      MP[ss] = mp(min(i + 1, num), max(i + 1, num2));
    }
  }
  for (auto a : MP) {
    ll po = a.S.F, po2 = a.S.S;
    if (po + mid <= po2) {
      return true;
    }
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  ll le = 0, ri = 222222;
  while (ri - le > 1) {
    ll mid = (le + ri) / 2;
    if (check(mid)) {
      le = mid;
    } else
      ri = mid;
  }
  cout << le << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll n;
string s;
bool hogege = true;
bool check(ll mid) {
  map<string, pair<ll, ll>> MP;
  for (ll i = 0; i <= s.size() - mid; i++) {
    string ss = SUBS(s, i + 1, i + mid);
    ll num = MP[ss].F, num2 = MP[ss].S;
    if (num == 0) {
      MP[ss] = mp(i + 1, i + 1);
    } else {
      MP[ss] = mp(min(i + 1, num), max(i + 1, num2));
    }
  }
  for (auto a : MP) {
    ll po = a.S.F, po2 = a.S.S;
    if (po + mid <= po2) {
      return true;
    }
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> s;
  ll le = 0, ri = n / 2 + 1;
  while (ri - le > 1) {
    ll mid = (le + ri) / 2;
    if (check(mid)) {
      le = mid;
    } else
      ri = mid;
  }
  cout << le << endl;
  return 0;
}
Problem p02913 (C++)

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

using namespace std;

void Main();
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

void Main() {
  auto &fin = cin;
  int64_t n;
  string s;
  fin >> n >> s;
  auto max_length_of_true_seq = [](const vector<bool> &v) -> int64_t {
    int64_t ret = 0;
    int64_t tmp = 0;
    for (int64_t i = 0; i < v.size(); ++i) {
      if (v[i]) {
        tmp++;
      } else {
        ret = max(tmp, ret);
        tmp = 0;
      }
    }
    ret = max(tmp, ret);
    return ret;
  };
  int64_t ans = 0;
  for (int64_t i = 0; i < n; ++i) {
    vector<bool> tmp(n - i);
    for (int64_t j = 0; j < n - 1; ++j) {
      tmp[j] = s[i + j] == s[j];
    }
    ans = max(ans, min(i, max_length_of_true_seq(tmp)));
  }
  cout << ans << endl;
}


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

using namespace std;

void Main();
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

void Main() {
  auto &fin = cin;
  int64_t n;
  string s;
  fin >> n >> s;
  auto max_length_of_true_seq = [](const vector<bool> &v) -> int64_t {
    int64_t ret = 0;
    int64_t tmp = 0;
    for (int64_t i = 0; i < v.size(); ++i) {
      if (v[i]) {
        tmp++;
      } else {
        ret = max(tmp, ret);
        tmp = 0;
      }
    }
    ret = max(tmp, ret);
    return ret;
  };
  int64_t ans = 0;
  for (int64_t i = 0; i < n; ++i) {
    vector<bool> tmp(n - i);
    for (int64_t j = 0; j < n - i; ++j) {
      tmp[j] = s[i + j] == s[j];
    }
    ans = max(ans, min(i, max_length_of_true_seq(tmp)));
  }
  cout << ans << endl;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define pf push_front
#define F first
#define S second
#define read(x) scanf("%d", &x)
int n;
char s[5005];
const ll H = 19260817, P = 998244353;
ll nxt[5005][5005];
int ans = 0;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> s + 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j)
      nxt[i][j] = (nxt[i][j - 1] + s[i + j - 1]) * H % P;
  }
  for (int i = 1; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (s[i] != s[j])
        continue;
      int L = 0, R = min(n - j + 1, j - i);
      if (nxt[i][R] == nxt[j][R]) {
        ans = max(ans, R);
        continue;
      }
      while (R - L > 1) {
        if (R <= ans)
          break;
        int M = L + R >> 1;
        if (nxt[i][M] == nxt[j][M])
          L = M;
        else
          R = M;
      }
      ans = max(ans, L);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define pf push_front
#define F first
#define S second
#define read(x) scanf("%d", &x)
int n;
char s[5005];
const ll H = 19260817, P = 998244353;
ll nxt[5005][5005];
int ans = 0;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> s + 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; i + j - 1 <= n; ++j)
      nxt[i][j] = (nxt[i][j - 1] + s[i + j - 1]) * H % P;
  }
  for (int i = 1; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (s[i] != s[j])
        continue;
      int L = 0, R = min(n - j + 1, j - i);
      if (nxt[i][R] == nxt[j][R]) {
        ans = max(ans, R);
        continue;
      }
      while (R - L > 1) {
        if (R <= ans)
          break;
        int M = L + R >> 1;
        if (nxt[i][M] == nxt[j][M])
          L = M;
        else
          R = M;
      }
      ans = max(ans, L);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02913 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()

dp = [[0] * (n + 1) for _ in range(n + 1)]

res = 0
for i in range(n - 1, -1, -1):
    for j in range(n - 1, i, -1):
        if s[i] == s[j]:
            dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1)
        res = max(res, min(dp[i][j], j - i))
print(res)


Fixed submission:
n = int(input())
s = input()

dp = [[0] * (n + 1) for _ in range(n + 1)]

res = 0
for i in range(n - 1, -1, -1):
    for j in range(n - 1, i, -1):
        if s[i] == s[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
            # dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1)
        res = max(res, min(dp[i][j], j - i))
print(res)
Problem p02913 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()
dp = [[0] * (N + 1) for _ in range(N + 1)]
res = 0

for i in range(N - 1, -1, -1):
    for j in range(N - 1, i, -1):
        if S[i] == S[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1, dp[i][j]
        res = max(res, min(j - i, dp[i][j]))

print(res)


Fixed submission:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()
dp = [[0] * (N + 1) for _ in range(N + 1)]
res = 0

for i in range(N - 1, -1, -1):
    for j in range(N - 1, i, -1):
        if S[i] == S[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
        res = max(res, min(j - i, dp[i][j]))

print(res)
Problem p02913 (Python)

Buggy submission (status: Time Limit Exceeded):
def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = min(i, k)
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))


Fixed submission:
def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = max(ret, min(i, k))
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))
Problem p02913 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        S = input().rstrip()

        half = N // 2
        for length in range(1, half + 1)[::-1]:
            i = 0
            already = set()
            while i + length < N:
                v = S[i : i + length]
                if v in already:
                    i += 1
                    continue

                if S.count(S[i : i + length]) >= 2:
                    print(length)
                    exit()
                else:
                    already.add(v)
                    i += 1
        print(0)


# Run main
if __name__ == "__main__":
    AtCoder().main()


Fixed submission:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        S = input().rstrip()

        dp = [[0 for i in range(N + 1)] for j in range(N + 1)]
        ans = 0
        for i in range(N)[::-1]:
            for j in range(N)[::-1]:
                if S[i] == S[j]:
                    dp[i][j] = min(dp[i + 1][j + 1] + 1, abs(i - j))
                    ans = max(ans, dp[i][j])
        print(ans)


# Run main
if __name__ == "__main__":
    AtCoder().main()
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N;
char S[5050];

ll ln[5050];
ll i, j, k, ans;

ll calc(ll b) {
  ll i = 1;
  ll j = 0;
  ll ret = 0;
  while (i + b < N) {
    // printf("%lld%lld\n", b,i);
    if (i + ln[i - j] < j + ln[j]) {
      ln[i] = ln[i - j];
      if (ret < min(i, ln[i])) {
        ret = min(i, ln[i]);
      }
    } else {
      ll k = j + ln[i] - i > 0 ? j + ln[i] - i : 0;
      while (i + k + b < N && S[k + b] == S[i + k + b])
        k++;
      ln[i] = k;
      if (ret < min(i, ln[i])) {
        ret = min(i, ln[i]);
      }
      j = i;
    }
    i++;
  }
  return ret;
}
int main() {
  scanf("%lld%s", &N, S);
  fornum(i, 0, N) {
    ll a = calc(i);
    // printf("%lld:%lld\n", i, a);
    if (ans < a)
      ans = a;
    ll j;
    fornum(j, 0, i) { ln[j] = 0ll; }
  }
  printf("%lld", ans);
  return 0;
}


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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N;
char S[5050];

ll ln[5050];
ll i, j, k, ans;

ll calc(ll b) {
  ll i = 1;
  ll j = 0;
  ll ret = 0;
  while (i + b < N) {
    // printf("%lld%lld\n", b,i);
    if (i + ln[i - j] < j + ln[j]) {
      ln[i] = ln[i - j];
      if (ret < min(i, ln[i])) {
        ret = min(i, ln[i]);
      }
    } else {
      ll k = j + ln[i] - i > 0 ? j + ln[i] - i : 0;
      while (i + k + b < N && S[k + b] == S[i + k + b])
        k++;
      ln[i] = k;
      if (ret < min(i, ln[i])) {
        ret = min(i, ln[i]);
      }
      j = i;
    }
    i++;
  }
  return ret;
}
int main() {
  scanf("%lld%s", &N, S);
  fornum(i, 0, N) {
    ll a = calc(i);
    // printf("%lld:%lld\n", i, a);
    if (ans < a)
      ans = a;
    ll j;
    fornum(j, 0, i) {}
  }
  printf("%lld", ans);
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// s[i:], s[j:] が何文字目まで一致するか
// 0 <= i < j < |S|
int solve(string &s, vector<vector<int>> memo, int i, int j) {
  if (i >= s.size() || j >= s.size() || s[i] != s[j]) {
    return 0;
  }

  if (memo[i][j] < 0) {
    memo[i][j] = min(j - i, solve(s, memo, i + 1, j + 1) + 1);
  }

  return memo[i][j];
}

int main(int argc, const char *argv[]) {
  int n;
  string s;
  cin >> n >> s;

  vector<vector<int>> memo(s.size(), vector<int>(s.size(), -1));
  int ans = 0;
  for (int i = 0; i < s.size() - 1; i++) {
    for (int j = i + 1; j < s.size(); j++) {
      ans = max(ans, solve(s, memo, i, j));
    }
  }

  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// s[i:], s[j:] が何文字目まで一致するか
// 0 <= i < j < |S|
int solve(string &s, vector<vector<int>> &memo, int i, int j) {
  if (i >= s.size() || j >= s.size() || s[i] != s[j]) {
    return 0;
  }

  if (memo[i][j] < 0) {
    memo[i][j] = min(j - i, solve(s, memo, i + 1, j + 1) + 1);
  }

  return memo[i][j];
}

int main(int argc, const char *argv[]) {
  int n;
  string s;
  cin >> n >> s;

  vector<vector<int>> memo(s.size(), vector<int>(s.size(), -1));
  int ans = 0;
  for (int i = 0; i < s.size() - 1; i++) {
    for (int j = i + 1; j < s.size(); j++) {
      ans = max(ans, solve(s, memo, i, j));
    }
  }

  cout << ans << '\n';
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int dp[5001][5001];

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  for (int i = N - 1; i >= 0; i--) {
    for (int j = N - 1; j >= 0; j--) {
      if (S[i] != S[j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i >= j) {
        continue;
      } else {
        int now = min(dp[i][j], j - i);
        ans = max(ans, now);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int dp[5001][5001];

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  for (int i = N - 1; i >= 0; i--) {
    for (int j = N - 1; j >= 0; j--) {
      if (S[i] != S[j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i >= j) {
        continue;
      } else {
        int now = min(dp[i][j], j - i);
        ans = max(ans, now);
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  string S;
  cin >> S;
  vvll a(26, vll(0));
  for (int i = 0; i < N; i++) {
    a.at(S.at(i) - 'a').push_back(i);
  }
  ll ans = 0LL;
  for (int i = 0; i < 26; i++) {
    int num = a.at(i).size();
    for (int j = 0; j < num - 1; j++) {
      for (int k = j + 1; k < num; k++) {
        ll counter = 0;
        int jj = a.at(i).at(j), kk = a.at(i).at(k);
        while (jj + counter < kk && kk + counter < N &&
               S[jj + counter] == S[kk + counter]) {
          counter++;
        }
        chmax(ans, counter);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  string S;
  cin >> S;
  vvll a(26, vll(0));
  for (int i = 0; i < N; i++) {
    a.at(S.at(i) - 'a').push_back(i);
  }
  ll ans = 0LL;
  for (int i = 0; i < 26; i++) {
    int num = a.at(i).size();
    for (int j = 0; j < num - 1; j++) {
      for (int k = j + 1; k < num; k++) {
        ll counter = 0;
        int jj = a.at(i).at(j), kk = a.at(i).at(k);
        if (jj - 1 >= 0 && kk - 1 >= jj + 1 && S[jj - 1] == S[kk - 1])
          continue;
        while (jj + counter < kk && kk + counter < N &&
               S[jj + counter] == S[kk + counter]) {
          counter++;
        }
        chmax(ans, counter);
      }
    }
  }
  cout << ans << endl;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  FOR(i, 1, n) {
    int tmp = 0;
    REP(j, n) {
      if (s[j] == s[j + i]) {
        tmp++;
        ans = max(ans, tmp);
        if (ans == i)
          break;
      } else {
        tmp = 0;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  FOR(i, 1, n) {
    int tmp = 0;
    REP(j, n - i) {
      if (s[j] == s[j + i]) {
        tmp++;
        ans = max(ans, tmp);
        if (ans == i)
          break;
      } else {
        tmp = 0;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)
#pragma GCC optimize("unroll-loops")

#define DEBUG(x) cout << ">> " << #x << ": " << x << endl;
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;

int main() {
  _ int n;
  cin >> n;
  string s;
  cin >> s;
  int maxx = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (n - j <= maxx)
        break;
      if (s[i] == s[j] && s[i + maxx] == s[j + maxx]) {
        int local = 1, len = j - i - 1;
        int l = i + 1, r = j + 1;
        while (s[l] == s[r] && len-- > 0) {
          l++;
          r++;
          local++;
        }
        maxx = max(maxx, local);
      }
    }
  }
  cout << maxx << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)
#pragma GCC optimize("unroll-loops")

#define DEBUG(x) cout << ">> " << #x << ": " << x << endl;
#define _                                                                      \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;

int main() {
  _ int n;
  cin >> n;
  string s;
  cin >> s;
  int maxx = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (n - j <= maxx)
        break;
      if (s[i] == s[j] && s[i + maxx] == s[j + maxx]) {
        int local = 1, len = j - i - 1;
        if (len + 1 <= maxx)
          continue;
        if (maxx != 0) {
          if (s[i + maxx - 1] != s[j + maxx - 1])
            continue;
        }
        int l = i + 1, r = j + 1;
        while (s[l] == s[r] && len-- > 0) {
          l++;
          r++;
          local++;
        }
        maxx = max(maxx, local);
      }
    }
  }
  cout << maxx << endl;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

typedef long long LL;
typedef long double LD;

void func() {
  LL N, K;
  cin >> N;
  string S;
  cin >> S;

  LL counter_max = 0;
  for (LL i = 0; i < N; ++i) {                     // 部分文字列1の先頭
    for (LL j = i + counter_max + 1; j < N; ++j) { // 部分文字列2の先頭
      LL counter = 0;
      for (LL k = 0; k + j < N; ++k) { // 何文字目か
        if (i + k < j) {
          if (S[i + k] == S[j + k]) {
            counter++;
          } else {
            break;
          }
        }
      }
      // cerr <<i<<" : "<<j <<" : " << counter << endl;
      counter_max = max(counter, counter_max);
    }
  }

  cout << counter_max;
}

int main() {
  func();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

typedef long long LL;
typedef long double LD;

void func() {
  LL N, K;
  cin >> N;
  string S;
  cin >> S;

  LL counter_max = 0;
  for (LL i = 0; i < N; ++i) {                     // 部分文字列1の先頭
    for (LL j = i + counter_max + 1; j < N; ++j) { // 部分文字列2の先頭
      if (i > 0 && S[i - 1] == S[j - 1]) {
        continue;
      }
      LL counter = 0;
      for (LL k = 0; k + j < N; ++k) { // 何文字目か
        if (i + k < j) {
          if (S[i + k] == S[j + k]) {
            counter++;
          } else {
            break;
          }
        }
      }
      // cerr <<i<<" : "<<j <<" : " << counter << endl;
      counter_max = max(counter, counter_max);
    }
  }

  cout << counter_max;
}

int main() {
  func();
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  int left = 0;
  int right = 1;
  int ans = 0;
  while (1) {
    string a = S.substr(left, right - left);
    string b = S.substr(right);
    // cout << a << " " << b << " " << left << " " << right << endl;
    if (b.find(a) != string::npos) {
      ans = max(ans, (int)a.size());
      right = min(right + 1, N - 1);
      if (left == N - 1)
        break;
    } else {
      left++;
      if (right == left)
        right = min(right + 1, N - 1);
      if (left == N - 1)
        break;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  int left = 0;
  int right = 1;
  int ans = 0;
  while (1) {
    if (N - left < ans + 2)
      break;
    string a = S.substr(left, right - left);
    string b = S.substr(right);
    // cout << a << " " << b << " " << left << " " << right << endl;
    if (b.find(a) != string::npos) {
      ans = max(ans, (int)a.size());
      right = min(right + 1, N - 1);
      if (left == N - 1)
        break;
    } else {
      left++;
      if (right == left)
        right = min(right + 1, N - 1);
      if (left == N - 1)
        break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02913 (C++)

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

#define EPS (1e-10)
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)
#define all(a) a.begin(), a.end()

using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvb = vector<vb>;

constexpr int MOD = 1000000007;

// O(n^2)
bool check(const string &s, int x) {
  int n = s.size();

  int cnt;
  rep(diff, x, n - x + 1) {
    cnt = 0;
    rep(i, 0, n - x) {
      // (i, i + diff)
      if (s[i] == s[i + diff]) {
        ++cnt;
        if (cnt == x) {
          return true;
        }
      } else {
        cnt = 0;
      }
    }
  }
  return false;
}

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

  int n;
  cin >> n;

  string s;
  cin >> s;

  int ok = 0, ng = n / 2 + 1;
  while (ok + 1 != ng) {
    int mid = (ok + ng) / 2;
    if (check(s, mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

  return 0;
}

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

#define EPS (1e-10)
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)
#define all(a) a.begin(), a.end()

using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvb = vector<vb>;

constexpr int MOD = 1000000007;

// O(n^2)
bool check(const string &s, int x) {
  int n = s.size();

  int cnt;
  rep(diff, x, n - x + 1) {
    cnt = 0;
    rep(i, 0, n - diff) {
      // (i, i + diff)
      if (s[i] == s[i + diff]) {
        ++cnt;
        if (cnt == x) {
          return true;
        }
      } else {
        cnt = 0;
      }
    }
  }
  return false;
}

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

  int n;
  cin >> n;

  string s;
  cin >> s;

  int ok = 0, ng = n / 2 + 1;
  while (ok + 1 != ng) {
    int mid = (ok + ng) / 2;
    if (check(s, mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

vector<vector<int>> cnt(26);

int main() {
  int n, f, a, b, t;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    cnt[s[i] - 'a'].push_back(i);
  }
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    f = cnt[i].size();
    for (int j = 0; j < f; j++) {
      a = cnt[i][j];
      for (int k = f - 1; k > j && cnt[i][k] - a > ans; k--) {
        b = cnt[i][k];
        t = 0;
        for (int m = 0; a + m < b; m++) {
          if (s[a + m] == s[b + m])
            t++;
          else
            break;
        }
        if (t > ans)
          ans = t;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

vector<vector<int>> cnt(26);

int main() {
  int n, f, a, b, t;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    cnt[s[i] - 'a'].push_back(i);
  }
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    f = cnt[i].size();
    for (int j = 0; j < f; j++) {
      a = cnt[i][j];
      for (int k = f - 1; k > j && cnt[i][k] - a > ans; k--) {
        b = cnt[i][k];
        if (n - b <= ans)
          continue;
        t = 0;
        for (int m = 0; a + m < b; m++) {
          if (s[a + m] == s[b + m])
            t++;
          else
            break;
        }
        if (t > ans)
          ans = t;
      }
    }
  }
  cout << ans << endl;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define MAXN 9300000

int p[MAXN][26], minn[MAXN];
int n, top, i, j, k, c, best, x;
string s;

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

  cin >> n >> s;
  best = 0;
  top = 0;
  for (i = 0; i < n; i++) {
    x = 0;
    for (j = 0; (i + j < n) && (j < n / 2); j++) {
      c = s[i + j] - 'a';
      if (p[x][c] == 0)
        p[x][c] = ++top;
      x = p[x][c];
      if (minn[x] == 0)
        minn[x] = i + 1;
      else {
        if (i + 1 - minn[x] >= j + 1)
          best = max(best, j + 1);
      }
    }
  }

  cout << best << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define MAXN 9500000

int p[MAXN][26], minn[MAXN];
int n, top, i, j, k, c, best, x;
string s;

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

  cin >> n >> s;
  best = 0;
  top = 0;
  for (i = 0; i < n; i++) {
    x = 0;
    for (j = 0; (i + j < n) && (j < n / 2); j++) {
      c = s[i + j] - 'a';
      if (p[x][c] == 0)
        p[x][c] = ++top;
      x = p[x][c];
      if (minn[x] == 0)
        minn[x] = i + 1;
      else {
        if (i + 1 - minn[x] >= j + 1)
          best = max(best, j + 1);
      }
    }
  }

  cout << best << "\n";

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
typedef vector<int> vi;
const ll M = 1e9 + 7;

template <typename T> vi z_algorithm(T &s, ll n) {
  vi res(n);
  res[0] = n;
  ll i1 = 1, i2 = 0;

  while (i1 < n) {
    while (i1 + i2 < n && s[i2] == s[i1 + i2])
      ++i2;
    res[i1] = i2;
    if (i2 == 0) {
      ++i1;
      continue;
    }
    ll i3 = 1;
    while (i1 + i3 < n && i3 + res[i3] < i2) {
      res[i1 + i3] = res[i3];
      ++i3;
    }
    i1 += i3, i2 -= i3;
  }
  return res;
}

int main(void) {
  int n, ans = 0;
  string s;
  cin >> n >> s;
  rep(i, n) {
    string ss = s.substr(i, n - i);
    vi res = z_algorithm(ss, n);
    rep(j, n - i) ans = max(ans, min(j, res[j]));
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
typedef vector<int> vi;
const ll M = 1e9 + 7;

template <typename T> vi z_algorithm(T &s, ll n) {
  vi res(n);
  res[0] = n;
  ll i1 = 1, i2 = 0;

  while (i1 < n) {
    while (i1 + i2 < n && s[i2] == s[i1 + i2])
      ++i2;
    res[i1] = i2;
    if (i2 == 0) {
      ++i1;
      continue;
    }
    ll i3 = 1;
    while (i1 + i3 < n && i3 + res[i3] < i2) {
      res[i1 + i3] = res[i3];
      ++i3;
    }
    i1 += i3, i2 -= i3;
  }
  return res;
}

int main(void) {
  int n, ans = 0;
  string s;
  cin >> n >> s;
  rep(i, n) {
    string ss = s.substr(i, n - i);
    vi res = z_algorithm(ss, n - i);
    rep(j, n - i) ans = max(ans, min(j, res[j]));
  }

  cout << ans << endl;

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <queue>

using namespace std;

char S[6000];

int main() {
  int N;
  int ret = 0;
  scanf("%d%s", &N, &S);
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      for (int k = 0; k < j - i && j + k < N && S[i + k] == S[j + k]; ++k) {
        if (ret < k + 1) {
          ret = k + 1;
        }
      }
    }
  }
  printf("%d\n", ret);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <queue>

using namespace std;

char S[6000];

int main() {
  int N;
  int ret = 0;
  scanf("%d%s", &N, &S);
  for (int i = 0; i < N; ++i) {
    for (int j = i + ret + 1; j + ret < N; ++j) {
      if (i + ret < j && S[i + ret] == S[j + ret]) {
        int k;
        for (k = 0; k < ret && S[i + k] == S[j + k]; ++k)
          ;
        if (k == ret) {
          for (; ret < j - i && j + ret < N && S[i + ret] == S[j + ret]; ++ret)
            ;
        }
      }
    }
  }
  printf("%d\n", ret);
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int M = 3005;
char s[M];
int nxt[M];
int get_nxt(char p[], int m, int nxt[]) {
  nxt[0] = m;
  int j = -1, a, l;
  int ans = 0;
  for (int i = 1; i < m; i++, j--) {
    if (j == -1 || i + nxt[i - a] >= l) {
      if (j == -1)
        j = 0, l = i;
      for (; l < m && j < m && p[l] == p[j]; l++, j++)
        ;
      nxt[i] = j;
      a = i;
    } else
      nxt[i] = nxt[i - a];
    // cout<<i<<" "<<nxt[i]<<endl;
    ans = max(ans, min(nxt[i], i));
  }
  return ans;
}

int main() {
  int n;
  scanf("%d%s", &n, s);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout<<i<<" : "<<endl;
    ans = max(ans, get_nxt(s + i, n - i, nxt));
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int M = 5005;
char s[M];
int nxt[M];
int get_nxt(char p[], int m, int nxt[]) {
  nxt[0] = m;
  int j = -1, a, l;
  int ans = 0;
  for (int i = 1; i < m; i++, j--) {
    if (j == -1 || i + nxt[i - a] >= l) {
      if (j == -1)
        j = 0, l = i;
      for (; l < m && j < m && p[l] == p[j]; l++, j++)
        ;
      nxt[i] = j;
      a = i;
    } else
      nxt[i] = nxt[i - a];
    // cout<<i<<" "<<nxt[i]<<endl;
    ans = max(ans, min(nxt[i], i));
  }
  return ans;
}

int main() {
  int n;
  scanf("%d%s", &n, s);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout<<i<<" : "<<endl;
    ans = max(ans, get_nxt(s + i, n - i, nxt));
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define for0(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int n, ans;
string s;
int main() {
  cin >> n >> s;
  for0(i, n) for0(j, i) {
    if (i - j < ans)
      continue;
    int p = j, q = i;
    while (s[p] == s[q]) {
      p++;
      q++;
      if (p == i)
        break;
      if (q == n)
        break;
    }
    ans = max(ans, p - j);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define for0(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int n, ans;
string s;
int main() {
  cin >> n >> s;
  for0(i, n) for0(j, i) {
    if (i - j < ans)
      continue;
    if (j > 0 && s[i - 1] == s[j - 1])
      continue;
    int p = j, q = i;
    while (s[p] == s[q]) {
      p++;
      q++;
      if (p == i)
        break;
      if (q == n)
        break;
    }
    ans = max(ans, p - j);
  }
  cout << ans << endl;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

ll n, dp[5000][5000];
string s;
int main() {
  cin >> n >> s;
  REP(i, n) {
    REP(j, n) {
      if (s[i] == s[j] and dp[i][j] + 1 <= j - i)
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
    }
  }
  ll ans = 0;
  REP(i, n + 1) {
    REP(j, n + 1) { ans = max(ans, dp[i][j]); }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

ll n, dp[5100][5100];
string s;
int main() {
  cin >> n >> s;
  REP(i, n) {
    REP(j, n) {
      if (s[i] == s[j] and dp[i][j] + 1 <= j - i)
        dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
    }
  }
  ll ans = 0;
  REP(i, n + 1) {
    REP(j, n + 1) { ans = max(ans, dp[i][j]); }
  }
  cout << ans << endl;
}