Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
S = input()

mod = (1 << 61) - 1
b = 10 ** 9 + 7

h_table = [[0] * (N + 1) for _ in range(N + 1)]
memo = defaultdict(list)
length = defaultdict(int)

for i, s in enumerate(S):
    h = ord(s)
    h_table[i][i + 1] = h
    memo[h].append((i, i + 1))
    length[h] = 1

for d in range(2, N + 1):
    for i, s in enumerate(S[d - 1:]):
        h = (h_table[i][i+d-1] * b + ord(s)) % mod
        h_table[i][i+d] = h
        memo[h].append((i, i+d))
        length[h] = d

ans = 0
for h, v in memo.items():
    if len(v) < 2:
        continue
    _, L = v[0]
    R, _ = v[-1]
    if L <= R:
        ans = length[h]

print(ans)

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
S = input()

mod = (1 << 61) - 1
b = 10 ** 9 + 7

h_table = [[0] * (N + 1) for _ in range(N + 1)]
memo = defaultdict(list)
length = defaultdict(int)

for i, s in enumerate(S):
    h = ord(s)
    h_table[i][i + 1] = h
    memo[h].append((i, i + 1))
    length[h] = 1

for d in range(2, N + 1):
    for i, s in enumerate(S[d - 1:]):
        h = (h_table[i][i+d-1] * b + ord(s)) % mod
        h_table[i][i+d] = h
        memo[h].append((i, i+d))
        length[h] = d

ans = 0
for h, v in memo.items():
    if len(v) < 2:
        continue
    _, L = v[0]
    R, _ = v[-1]
    if L <= R:
        ans = length[h]

print(ans)

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def e():
    n=int(input())
    s=input()
    ans=0

    for i in range(n):
        cnt = 0
        for j in range(n-i):
            if s[j] == s[i+j]:
                cnt+=1
            else:
                cnt = 0
            ans = max(ans, min(i, cnt))
    return ans


print(e())

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
S=input()
dp=[[0]*(n+1) for _ in range(n+1)]

for i in range(n-1, -1, -1):
    for j in range(n-1, -1, -1):
        if S[i] != S[j]:
            continue
        else:
            dp[i][j] = dp[i+1][j+1] + 1

ans=0
for i in range(n):
    for j in range(n):
        if i>=j:
            continue
        now = min(dp[i][j], j-i)
        ans = max(ans, now)

print(ans)

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
S=input()

def e():
    for l in range(n//2, 0, -1):
        for i in range(n-l+1):
            if n-(l+i)<l:
                break
            s=S[i:i+l]
            if len(S.split(s))>=3:
                return l
    return 0


print(e())

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
S=input()

def e():
    for l in range(n//2, 0, -1):
        for i in range(n-l+1):
            s=S[i:i+l]
            if len(S.split(s))>=3:
                return l
    return 0

print(e())

Fixed submission:
N = int(input())
S = input()

dp = [[0] * N for _ in range(N)]

ans = 0

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

print(ans)