Buggy python submission (verdict: Time Limit Exceeded):
def strTint(s):
    return ord(s)-96

alpha = {}
for i in range(97,97+26):
    alpha[chr(i)] = i - 96

def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    
    b = 27
    mod1 = 10**9+7
    mod2 = 10**9+9
    D = {}
    for i in range(N):
        H = strTint(S[i])
        H1 = H%mod1
        #H2 = H%mod2
        if not H1 in D:
            D[H1] = [1,i,i]
        for j in range(i+1,min(i+N//2+2,N)):
            H1 = (H1*b + strTint(S[j]))%mod1
            if not H1 in D:
                D[H1] = [j-i+1,i,i]
            else:
                D[H1][-1] = i
    
    ans = 0
    for i in D:
        L = D[i][0]
        tmp = 0
        if D[i][1] + L <= D[i][-1] and ans < L:
            ans = L
    g = time.time()
    #print(g-star)
    print(ans)
    
main()

Fixed submission:
def z_algo(S):
    N = len(S)
    A = [0]*N
    i = 1; j = 0
    A[0] = l = len(S)
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A

def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    A = [[] for i in range(N)]
    for i in range(N):
        A[i] = [0]*i + z_algo(S[i:])
    ans = 0
    for i in range(N):
        for j in range(N):
            if i + A[i][j] <= j and ans < A[i][j]:
                ans = A[i][j]
    
    g = time.time()
    #print(g-star)
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def strTint(s):
    return ord(s)-96
def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    
    b = 27
    mod1 = 10**9+7
    mod2 = 10**9+9
    D = {}
    for i in range(N):
        H = strTint(S[i])
        H1 = H%mod1
        H2 = H%mod2
        if not (H1,H2) in D:
            D[(H1,H2)] = [1,i,i]
        for j in range(i+1,min(i+N//2+2,N)):
            H1 = (H1*b + strTint(S[j]))%mod1
            H2 = (H2*b + strTint(S[j]))%mod2
            if not (H1,H2) in D:
                D[(H1,H2)] = [j-i+1,i,i]
            else:
                D[(H1,H2)][-1] = i
    
    ans = 0
    for i in D:
        L = D[i][0]
        tmp = 0
        if D[i][1] + L <= D[i][-1] and ans < L:
            ans = L
    g = time.time()
    #print(g-star)
    print(ans)
    
main()

Fixed submission:
def z_algo(S):
    N = len(S)
    A = [0]*N
    i = 1; j = 0
    A[0] = l = len(S)
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A

def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    A = [[] for i in range(N)]
    for i in range(N):
        A[i] = [0]*i + z_algo(S[i:])
    ans = 0
    for i in range(N):
        for j in range(N):
            if i + A[i][j] <= j and ans < A[i][j]:
                ans = A[i][j]
    
    g = time.time()
    #print(g-star)
    print(ans)
    
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    #N = 2000
    #S = "a"*N
    import time
    star = time.time()
    
    from collections import defaultdict
    D = {}
     
    for i in range(N):
        s = S[i]
        if not s in D:
            D[s] = [1, i, i]
        else:
            D[s][-1] = i
        for j in range(i+1,min(i+2+N//2, N, max(2*i+2,(N+i)//2+2))):
            s = "".join([s,S[j]])
            if not s in D:
                D[s] = [j-i+1, i, i]
            else:
                D[s][-1] = i
    totyuu = time.time()
    #print(totyuu-star)
        
    ans = 0
    for i in D:
        L = D[i][0]
        tmp = 0
        if len(D[i]) >= 3:
            if D[i][1] + L <= D[i][-1]:
                tmp = L
        if ans < tmp:
            ans = tmp
    g = time.time()
    #print(g-star)
    print(ans)
    
main()



Fixed submission:
def z_algo(S):
    N = len(S)
    A = [0]*N
    i = 1; j = 0
    A[0] = l = len(S)
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A

def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    A = [[] for i in range(N)]
    for i in range(N):
        A[i] = [0]*i + z_algo(S[i:])
    ans = 0
    for i in range(N):
        for j in range(N):
            if i + A[i][j] <= j and ans < A[i][j]:
                ans = A[i][j]
    
    g = time.time()
    #print(g-star)
    print(ans)
    
main()

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

from collections import defaultdict
D = defaultdict(list)

for i in range(N):
    for j in range(i+1,N+1):
        if D[S[i:j]] == []:
            D[S[i:j]].append(j-i)
        D[S[i:j]].append(i)
        
ans = 0
for i in D:
    L = D[i][0]
    tmp = 0
    if len(D[i]) >= 3:
        if D[i][1] + L <= D[i][-1]:
            tmp = L
    if ans < tmp:
        ans = tmp
print(ans)

Fixed submission:
def z_algo(S):
    N = len(S)
    A = [0]*N
    i = 1; j = 0
    A[0] = l = len(S)
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A

def main():
    N = int(input())
    S = input()
    #N = 5000
    #S = "a"*N
    import time
    star = time.time()
    A = [[] for i in range(N)]
    for i in range(N):
        A[i] = [0]*i + z_algo(S[i:])
    ans = 0
    for i in range(N):
        for j in range(N):
            if i + A[i][j] <= j and ans < A[i][j]:
                ans = A[i][j]
    
    g = time.time()
    #print(g-star)
    print(ans)
    
main()