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

ans = [0]
lenab = len(ab)

for h in range(lenab):
    abc = ab[h:]
    lenabc = len(abc)
    i = 1; j = 0
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < i:
            j += 1
        if j == 0:
            i += 1
        else:
            ans.append(j)
            i += 1
            j=0

print(max(ans))

Fixed submission:

n = int(input())
ab = str(input())

ans = set([0])
lenab = len(ab)

for h in range(lenab):
    abc = ab[h:]
    lenabc = len(abc)
    i = 1; j = 0
    A = [0]*lenabc
    A[0] = lenabc
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < i:
            j += 1
        if j == 0:
            i += 1
        else:
            A[i] = j
            ans.add(j)
            k = 1
            while lenabc - i > k < j - A[k] and j < i - k :
                A[i + k] = A[k]
                k += 1
            i += k;
            j -= k

print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):

n = int(input())
ab = str(input())

ans = 0
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] and 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

lenab = len(ab)
for i in range(0, lenab):
    if ans > lenab - i - 1:
        break
    S = ab[i:]
    o = z_algo(S)
    p = o[1:]
    p.append(ans)
    ans = max(p)

print(ans)

Fixed submission:

n = int(input())
ab = str(input())

ans = set([0])
lenab = len(ab)

for h in range(lenab):
    abc = ab[h:]
    lenabc = len(abc)
    i = 1; j = 0
    A = [0]*lenabc
    A[0] = lenabc
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < i:
            j += 1
        if j == 0:
            i += 1
        else:
            A[i] = j
            ans.add(j)
            k = 1
            while lenabc - i > k < j - A[k] and j < i - k :
                A[i + k] = A[k]
                k += 1
            i += k;
            j -= k

print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):

n = int(input())
ab = str(input())
a = list(ab)
b = list(range(0, len(a)+1))

e = list(zip(a,b))
visited = []
count = [0]
while len(e) >0:
    x,y = e.pop()
    lenx = len(x)
    z = ab[y+lenx:].find(x)
    if z == -1:
        visited.append((x,y))
    else:
        f = y+z+1
        g = (x, y)
        count.append(lenx)
        visited.append(g)

        h = (ab[y:y+lenx+1], y)
        e.insert(0, h)

print(count[-1])

Fixed submission:

n = int(input())
ab = str(input())

ans = set([0])
lenab = len(ab)

for h in range(lenab):
    abc = ab[h:]
    lenabc = len(abc)
    i = 1; j = 0
    A = [0]*lenabc
    A[0] = lenabc
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < i:
            j += 1
        if j == 0:
            i += 1
        else:
            A[i] = j
            ans.add(j)
            k = 1
            while lenabc - i > k < j - A[k] and j < i - k :
                A[i + k] = A[k]
                k += 1
            i += k;
            j -= k

print(max(ans))