Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
ba = ord("a")
for i in range(N):
  S[i] = ord(S[i]) - ba + 1
B = 27
mod = 10 ** 11 + 7
def contain(a, b):
  al = len(a)
  bl = len(b)
  #print(al, a)
  if al > bl: return False
  t = pow(B, al)
  ah = 0; bh = 0
  for i in range(al):
    ah = ah * B + a[i]
    ah %= mod
    bh = bh * B + b[i]
    bh %= mod
  for i in range(bl - al + 1):
    if ah == bh: return True
    if i + al < bl:
      #print(i, i + al)
      bh = bh * B + b[i + al] - b[i] * t
      bh %= mod
  return False
res = 0
for i in range(N):
  r = min(i + N // 2, N + 1)
  l = i + res + 1
  m = (l + r) // 2
  if l > r: continue
  while r - l > 3:
    m = (l + r) // 2
    if contain(S[i: m], S[m: ]):
      l = m
      res = max(res, m - i)
    else:
      r = m
  if m <= N:
    if contain(S[i: m + 1], S[m + 1: ]):
      res = max(res, m + 1 - i)
  if m + 1 <= N:
    if contain(S[i: m + 2], S[m + 2: ]):
      res = max(res, m + 2 - i)
  if m + 2 <= N:
    if contain(S[i: m + 3], S[m + 3: ]):
      res = max(res, m + 3 - i)
print(res)

Fixed submission:
N = int(input())
S = list(input())
#LCSRe i番目とj番目でそれぞれ終わるような共通文字列の長さ
#i < j であるi文字目とj文字目が等しいならLCSRe[i + 1][j + 1] = LCSRe[i][j] + 1
#res_lengthよりLCSReの更新した値が大なら、その時のindexをiへ、res_lengthも更新

def longestRepeatedSubstring(s): 
  n = len(s) 
  LCSRe = [[0 for _ in range(n + 1)] for _ in range(n + 1)] 
  res = "" 
  res_length = 0
  index = 0
  for i in range(1, n + 1): 
    for j in range(i + 1, n + 1): 
      if s[i - 1] == s[j - 1] and LCSRe[i - 1][j - 1] < j - i: 
        LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1
        if LCSRe[i][j] > res_length: 
          res_length = LCSRe[i][j] 
          index = max(i, index) 
      else: 
        LCSRe[i][j] = 0
  #print(LCSRe)
  if res_length > 0: 
    for i in range(index - res_length + 1, index + 1): 
      res = res + s[i - 1] 
  return res_length
print(longestRepeatedSubstring(S))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
ba = ord("a")
for i in range(N):
  S[i] = ord(S[i]) - ba + 1
B = 27
mod = 10 ** 11 + 7
def contain(a, b):
  al = len(a)
  bl = len(b)
  #print(al, a)
  if al > bl: return False
  t = pow(B, al)
  ah = 0; bh = 0
  for i in range(al):
    ah = ah * B + a[i]
    bh = bh * B + b[i]
  for i in range(bl - al + 1):
    if ah == bh: return True
    if i + al < bl:
      #print(i, i + al)
      bh = bh * B + b[i + al] - b[i] * t
  return False
res = 0
for i in range(N):
  r = min(i + N // 2, N + 1)
  l = i + 1
  m = (l + r) // 2
  while r - l > 3:
    r = min(i + N // 2, N + 1)
    l = i + 1
    m = (l + r) // 2
    if contain(S[i: m], S[m: ]):
      l = m
      res = max(res, m - i)
    else:
      r = m
  if m <= N:
    if contain(S[i: m + 1], S[m + 1: ]):
      res = max(res, m + 1 - i)
  if m + 1 <= N:
    if contain(S[i: m + 2], S[m + 2: ]):
      res = max(res, m + 2 - i)
  if m + 2 <= N:
    if contain(S[i: m + 3], S[m + 3: ]):
      res = max(res, m + 3 - i)
print(res)

Fixed submission:
N = int(input())
S = list(input())
#LCSRe i番目とj番目でそれぞれ終わるような共通文字列の長さ
#i < j であるi文字目とj文字目が等しいならLCSRe[i + 1][j + 1] = LCSRe[i][j] + 1
#res_lengthよりLCSReの更新した値が大なら、その時のindexをiへ、res_lengthも更新

def longestRepeatedSubstring(s): 
  n = len(s) 
  LCSRe = [[0 for _ in range(n + 1)] for _ in range(n + 1)] 
  res = "" 
  res_length = 0
  index = 0
  for i in range(1, n + 1): 
    for j in range(i + 1, n + 1): 
      if s[i - 1] == s[j - 1] and LCSRe[i - 1][j - 1] < j - i: 
        LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1
        if LCSRe[i][j] > res_length: 
          res_length = LCSRe[i][j] 
          index = max(i, index) 
      else: 
        LCSRe[i][j] = 0
  #print(LCSRe)
  if res_length > 0: 
    for i in range(index - res_length + 1, index + 1): 
      res = res + s[i - 1] 
  return res_length
print(longestRepeatedSubstring(S))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
ba = ord("a")
for i in range(N):
  S[i] = ord(S[i]) - ba + 1
B = 27
mod = 10 ** 11 + 7
def contain(a, b):
  al = len(a)
  bl = len(b)
  #print(al, a)
  if al > bl: return False
  t = pow(B, al)
  ah = 0; bh = 0
  for i in range(al):
    ah = ah * B + a[i]
    bh = bh * B + b[i]
  for i in range(bl - al + 1):
    if ah == bh: return True
    if i + al < bl:
      #print(i, i + al)
      bh = bh * B + b[i + al] - b[i] * t
  return False
res = 0
for i in range(N):
  r = min(i + N // 2, N + 1)
  l = i + 1
  m = (l + r) // 2
  while r - l > 3:
    r = min(i + N // 2, N + 1)
    l = i + 1
    m = (l + r) // 2
    if contain(S[i: m], S[m: ]):
      l = m
      res = max(res, m - i)
    else:
      r = m
  if m <= N:
    if contain(S[i: m + 1], S[m + 1: ]):
      res = max(res, m + 1 - i)
  if m + 1 <= N:
    if contain(S[i: m + 2], S[m + 2: ]):
      res = max(res, m + 2 - i)
  if m + 2 <= N:
    if contain(S[i: m + 3], S[m + 3: ]):
      res = max(res, m + 3 - i)
print(res)

Fixed submission:
N = int(input())
S = list(input())
#LCSRe i番目とj番目でそれぞれ終わるような共通文字列の長さ
#i < j であるi文字目とj文字目が等しいならLCSRe[i + 1][j + 1] = LCSRe[i][j] + 1
#res_lengthよりLCSReの更新した値が大なら、その時のindexをiへ、res_lengthも更新

def longestRepeatedSubstring(s): 
  n = len(s) 
  LCSRe = [[0 for _ in range(n + 1)] for _ in range(n + 1)] 
  res = "" 
  res_length = 0
  index = 0
  for i in range(1, n + 1): 
    for j in range(i + 1, n + 1): 
      if s[i - 1] == s[j - 1] and LCSRe[i - 1][j - 1] < j - i: 
        LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1
        if LCSRe[i][j] > res_length: 
          res_length = LCSRe[i][j] 
          index = max(i, index) 
      else: 
        LCSRe[i][j] = 0
  #print(LCSRe)
  if res_length > 0: 
    for i in range(index - res_length + 1, index + 1): 
      res = res + s[i - 1] 
  return res_length
print(longestRepeatedSubstring(S))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
s = set()
res = 0
for i in range(N):
  for j in range(i, N):
    if j - i + 1 > N // 2:
      break
    p = S[i: j + 1]
    if p in s:
      res = max(res, len(p))
    else:
      s.add(p)
print(res)

Fixed submission:
N = int(input())
S = list(input())
#LCSRe i番目とj番目でそれぞれ終わるような共通文字列の長さ
#i < j であるi文字目とj文字目が等しいならLCSRe[i + 1][j + 1] = LCSRe[i][j] + 1
#res_lengthよりLCSReの更新した値が大なら、その時のindexをiへ、res_lengthも更新

def longestRepeatedSubstring(s): 
  n = len(s) 
  LCSRe = [[0 for _ in range(n + 1)] for _ in range(n + 1)] 
  res = "" 
  res_length = 0
  index = 0
  for i in range(1, n + 1): 
    for j in range(i + 1, n + 1): 
      if s[i - 1] == s[j - 1] and LCSRe[i - 1][j - 1] < j - i: 
        LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1
        if LCSRe[i][j] > res_length: 
          res_length = LCSRe[i][j] 
          index = max(i, index) 
      else: 
        LCSRe[i][j] = 0
  #print(LCSRe)
  if res_length > 0: 
    for i in range(index - res_length + 1, index + 1): 
      res = res + s[i - 1] 
  return res_length
print(longestRepeatedSubstring(S))