Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
P = 97

def solve(N, S):
    A = [ord(c) - ord('a') + 2 for c in S]
    H = [0]
    pwr = 1
    for x in A:
        H.append(H[-1] + x * pwr)
        H[-1] %= MOD
        pwr = pwr * P % MOD

    def gethash(i, j):
        ha = H[j] - H[i]
        ha *= pow(pow(P, i, MOD), MOD-2, MOD)
        return ha % MOD

    def match(L):
        if L == 0: return True
        for i in xrange(N - L + 1):
            h1 = gethash(i, i+L)
            for j in xrange(i + L, N - L + 1):
                if h1 == gethash(j, j+L) and A[i:i+L] == A[j:j+L]:
                    return True

    #print map(doublematch, range(len(A)))
    lo, hi = 0, len(A) / 2
    while lo < hi:
        mi = (lo + hi + 1) / 2
        if match(mi):
            lo = mi
        else:
            hi = mi - 1
    return lo

print solve(int(raw_input()), raw_input())


Fixed submission:
rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())

MOD = 10**9 + 7
P = 97
def solve(N, S):
    A = [0]
    rawA = []
    #powers = []
    ha = 0
    #pwr = 1
    for c in S:
        x = ord(c) - ord('a') + 2
        rawA.append(x)
        #ha += x * pwr
        #ha %= MOD
        #powers.append(pwr)
        ha += x
        A.append(ha)
        #pwr *= P
        #pwr %= MOD
    #powers.append(pwr)

    def gethash(i, j):
        ha = A[j] - A[i]
        #ha *= pow(powers[i-1], MOD-2, MOD)
        ha %= MOD
        #print 'gh', i,j, ';', ha
        return ha

    # A
    #print 'g1',gethash(2, 2+5)
    #print 'g2', gethash(8, 8+5)
    
    def doublematch(L):
        if L == 0: return True
        for i in xrange(N - L + 1):
            for j in xrange(i + L, N - L + 1):
                if gethash(i, i+L) == gethash(j, j+L) and rawA[i:i+L] == rawA[j:j+L]:
                    return True

    #print map(doublematch, range(len(A)))
    lo, hi = 0, len(A) / 2
    while lo < hi:
        mi = (lo + hi + 1) / 2
        if doublematch(mi):
            lo = mi
        else:
            hi = mi - 1
    return lo

print solve(rri(), rr())

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
P = 97

def solve(N, S):
    A = [ord(c) - ord('a') + 2 for c in S]
    H = [0]
    pwr = 1
    for x in A:
        H.append(H[-1] + x * pwr)
        H[-1] %= MOD
        pwr = pwr * P % MOD

    def gethash(i, j):
        ha = H[j] - H[i]
        ha *= pow(pow(P, i, MOD), MOD-2, MOD)
        return ha % MOD

    def match(L):
        if L == 0: return True
        for i in xrange(N - L + 1):
            h1 = gethash(i, i+L)
            for j in xrange(i + L, N - L + 1):
                if h1 == gethash(j, j+L) and A[i:i+L] == A[j:j+L]:
                    return True

    #print map(doublematch, range(len(A)))
    lo, hi = 0, len(A) / 2
    while lo < hi:
        mi = (lo + hi + 1) / 2
        if match(mi):
            lo = mi
        else:
            hi = mi - 1
    return lo

print solve(int(raw_input()), raw_input())


Fixed submission:
rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())

MOD = 10**9 + 7
P = 97
def solve(N, S):
    A = [0]
    rawA = []
    #powers = []
    ha = 0
    #pwr = 1
    for c in S:
        x = ord(c) - ord('a') + 2
        rawA.append(x)
        #ha += x * pwr
        #ha %= MOD
        #powers.append(pwr)
        ha += x
        A.append(ha)
        #pwr *= P
        #pwr %= MOD
    #powers.append(pwr)

    def gethash(i, j):
        ha = A[j] - A[i]
        #ha *= pow(powers[i-1], MOD-2, MOD)
        ha %= MOD
        #print 'gh', i,j, ';', ha
        return ha

    # A
    #print 'g1',gethash(2, 2+5)
    #print 'g2', gethash(8, 8+5)
    
    def doublematch(L):
        if L == 0: return True
        for i in xrange(N - L + 1):
            for j in xrange(i + L, N - L + 1):
                if gethash(i, i+L) == gethash(j, j+L) and rawA[i:i+L] == rawA[j:j+L]:
                    return True

    #print map(doublematch, range(len(A)))
    lo, hi = 0, len(A) / 2
    while lo < hi:
        mi = (lo + hi + 1) / 2
        if doublematch(mi):
            lo = mi
        else:
            hi = mi - 1
    return lo

print solve(rri(), rr())