Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

# dp[i][j] = i文字目からとj文字目からとで最長の長さ
# S[i] != S[j] ならdp[i][j] = 0
# S[i] == S[j] ならdp[i][j] = dp[i + 1][j + 1] + 1

dp = [[0] * (N + 1) for i in range(N + 1)]

ans = 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
      ans = max(ans, min(dp[i][j], j - i))
print(ans)

Fixed submission:
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

def zalgo(S):
  N = len(S)
  res = [0] * N
  res[0] = N
  i = 1
  j = 0
  while i < N:
    while i + j < N and S[j] == S[i + j]:
      j += 1
    res[i] = j
    if j == 0:
      i += 1
      continue
    k = 1
    while i + k < N and k + res[k] < j:
      res[i + k] = res[k]
      k += 1
    i += k
    j -= k
  return res

res = 0
for i in range(N):
  T = S[i:]
  lcp = zalgo(T)
  for j in range(len(T)):
    l = min(lcp[j],j)
    if res < l:
      res = l

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

def zalgo(S):
  N = len(S)
  res = [0] * N
  res[0] = N
  i = 1
  j = 0
  while i < N:
    while i + j < N and S[j] == S[i + j]:
      j += 1
    res[i] = j
    if j == 0:
      i += 1
      continue
    k = 1
    while i + k < N and k + res[k] < j:
      res[i + k] = res[k]
      k += 1
    i += k
    j -= k
  return res

res = 0
for i in range(N):
  T = S[i:]
  lcp = zalgo(T)
  for j in range(len(T)):
    l = min(lcp[j],j)
    if res < l:
      res = l

print(res)

Fixed submission:
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

def zalgo(S):
  N = len(S)
  res = [0] * N
  res[0] = N
  i = 1
  j = 0
  while i < N:
    while i + j < N and S[j] == S[i + j]:
      j += 1
    res[i] = j
    if j == 0:
      i += 1
      continue
    k = 1
    while i + k < N and k + res[k] < j:
      res[i + k] = res[k]
      k += 1
    i += k
    j -= k
  return res

res = 0
for i in range(N):
  T = S[i:]
  lcp = zalgo(T)
  for j in range(len(T)):
    l = min(lcp[j],j)
    if res < l:
      res = l

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# 左の文字を決め打ちして、右の文字を試していく 右の文字の候補の位置はあらかじめdicで記録

from collections import defaultdict
pos = defaultdict(list)

import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

for i in range(N):
  pos[S[i]].append(i)

import bisect
def find_tar(s,i): # i + 1文字目以降からsを探す
#  print("pos[s]",pos[s],"から",i+1,"番目以降の最小値を探す")
  j = bisect.bisect_left(pos[s],i+1)
  if j == len(pos[s]):
    return None
  return pos[s][j:] # 全リストを返す

ans = 0
i = 0
while i < N:
  rest = find_tar(S[i],i)
  if rest is None:
    i += 1
    continue
  bestfit = 1
#  print(S[i],rest)
  for j in rest:
    # i,jに同じ文字がある状態
    fit = 1
#    print("i",i,S[i],"j",j,S[j],"からスタート")
    while i + fit < j and j + fit < N:
      if S[i + fit] == S[j + fit]:
#        print("fit",fit,"一致",S[i + fit])
        fit += 1
      else:
        break
    if bestfit < fit:
      bestfit = fit
    if bestfit > ans:
      ans = bestfit
#  print("i",i,"を",i + bestfit,"まで進める")
  i += 1 # i += bestfit

print(ans)

Fixed submission:
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

def zalgo(S):
  N = len(S)
  res = [0] * N
  res[0] = N
  i = 1
  j = 0
  while i < N:
    while i + j < N and S[j] == S[i + j]:
      j += 1
    res[i] = j
    if j == 0:
      i += 1
      continue
    k = 1
    while i + k < N and k + res[k] < j:
      res[i + k] = res[k]
      k += 1
    i += k
    j -= k
  return res

res = 0
for i in range(N):
  T = S[i:]
  lcp = zalgo(T)
  for j in range(len(T)):
    l = min(lcp[j],j)
    if res < l:
      res = l

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=input()
i=0
ans=0
while i<len(s):
  for j in range(i+1,len(s)):
    k=0
    while i+k<j and j+k<len(s):
      if s[i+k]==s[j+k]:
        k+=1
      else:
        break
    if ans<k:
      ans=k
  i+=1
print(ans)

Fixed submission:
import sys
readline = sys.stdin.readline

N = int(readline())
S = readline().rstrip()

def zalgo(S):
  N = len(S)
  res = [0] * N
  res[0] = N
  i = 1
  j = 0
  while i < N:
    while i + j < N and S[j] == S[i + j]:
      j += 1
    res[i] = j
    if j == 0:
      i += 1
      continue
    k = 1
    while i + k < N and k + res[k] < j:
      res[i + k] = res[k]
      k += 1
    i += k
    j -= k
  return res

res = 0
for i in range(N):
  T = S[i:]
  lcp = zalgo(T)
  for j in range(len(T)):
    l = min(lcp[j],j)
    if res < l:
      res = l

print(res)