Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    ans = 0
    for k in range(N):
        now_S = S[k:]
        ans_length = [-1] * (len(now_S)+1)
        j = -1
        for i, c in enumerate(now_S):
            left_end = now_S[j]
            right_end = c
            while j >= 0 and left_end != right_end:
                j = ans_length[j]
                left_end = now_S[j]
            j += 1
            ans_length[i+1] = j

        for i, length in enumerate(ans_length):
            tmp_max_length = i // 2
            if length <= tmp_max_length:
                ans = max(ans, length)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline
exit = sys.exit


def main():
    N = int(input().strip())
    S = input().strip()
    half_N = N//2
    cand = []
    flag = True
    cappend = cand.append
    for i in range(N):
        tmp = []
        tappend = tmp.append
        for j in range(1, N-i-1):
            if S[i] == S[i+j]:
                flag = False
                tappend(i+j)
        cappend(tmp)
    if flag:
        print(0)
        exit()

    for length in range(1, N//2+1):
        flag = True
        new_cand = []
        ncappend = new_cand.append
        for i in range(N-length):
            tmp = []
            now_check = S[i+length]
            tappend = tmp.append
            for c in cand[i]:
                if i + length >= c or c + length >= N:
                    continue
                if S[c+length] == now_check:
                    flag = False
                    tappend(c)
            ncappend(tmp)
        cand = new_cand
        if flag:
            print(length)
            exit()

    print(length+1)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline
exit = sys.exit
def main():
    N = int(input().strip())
    S = input().strip()
    half_N = N//2
    cand = []
    flag = True
    for i in range(N):
        tmp = []
        for j in range(1, N-i-1):
            if S[i] == S[i+j]:
                flag = False
                tmp.append(i+j)
        cand.append(tmp)
    if flag:
        print(0)
        exit()

    for length in range(1, N//2+1):
        flag = True
        new_cand = []
        for i in range(N-length):
            tmp = []
            now_check = S[i+length]
            for c in cand[i]:
                if i + length >= c or c + length >= N:
                    continue
                if S[c+length] == now_check:
                    flag = False
                    tmp.append(c)
            new_cand.append(tmp)
        cand = new_cand
        if flag:
            print(length)
            exit()

    print(length+1)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline
exit = sys.exit
N = int(input().strip())
S = input().strip()
half_N = N//2
cand = []
flag = True
for i in range(N):
    tmp = []
    for j in range(1, N-i-1):
        if S[i] == S[i+j]:
            flag = False
            tmp.append(i+j)
    cand.append(tmp)
if flag:
    print(0)
    exit()

for length in range(1, N//2+1):
    flag = True
    new_cand = []
    for i in range(N-length):
        tmp = []
        now_check = S[i+length]
        for c in cand[i]:
            if i + length >= c or c + length >= N:
                continue
            if S[c+length] == now_check:
                flag = False
                tmp.append(c)
        new_cand.append(tmp)
    cand = new_cand
    if flag:
        print(length)
        exit()

print(length+1)


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
S = input()
half_N = N//2
cand = [[] for _ in range(N-1)]
flag = True
for i in range(N):
    for j in range(1, N-i-1):
        if S[i] == S[i+j]:
            flag = False
            cand[i].append(i+j)
if flag:
    print(0)
    exit()

for length in range(1, N//2+1):
    flag = True
    new_cand = [[] for _ in range(N-1)]
    for i in range(N-length):
        tmp = []
        now_check = S[i+length]
        for c in cand[i]:
            if i + length >= c or c + length >= N:
                continue
            if S[c+length] == now_check:
                flag = False
                tmp.append(c)
        new_cand[i] = tmp
    cand = new_cand
    if flag:
        print(length)
        exit()
print(length+1)


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
S = input()
half_N = N//2
cand = [[] for _ in range(N-1)]
flag = True
for i in range(N):
    for j in range(1, N-i-1):
        if S[i] == S[i+j]:
            flag = False
            cand[i].append(i+j)
if flag:
    print(0)
    exit()

for length in range(1, N//2+1):
    flag = True
    new_cand = [[] for _ in range(N-1)]
    for i in range(N-length):
        tmp = []
        now_check = S[i+length]
        for c in cand[i]:
            if i + length >= c or c + length >= N:
                continue
            if S[c+length] == now_check:
                flag = False
                tmp.append(c)
        new_cand[i] = tmp
    cand = new_cand
    if flag:
        print(length)
        exit()
print(length+1)


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
S = input()
half_N = N//2
for i in range(half_N):
    now_len = half_N - i
    #print(now_len)
    dic = defaultdict(list)

    for j in range(N - now_len+1):
        dic[S[j:j+now_len]].append(j)

    for key, val in dic.items():
        #print(key)
        if len(val) > 1:
            if max(val)-min(val) >= now_len:
                print(now_len)
                exit()
print(0)


Fixed submission:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    dp = [0] * N
    for i, s in enumerate(S):
        if i == 0:
            continue
        before_ans = dp[i-1]
        now_S = S[:i+1]
        tmp_ans = now_S[-(before_ans+1):i+1]
        # print(i, before_ans, tmp_ans)
        if tmp_ans in now_S[:-(before_ans+1)]:
            dp[i] = before_ans + 1
        else:
            dp[i] = before_ans
    print(dp[-1])


if __name__ == "__main__":
    main()