Problem: Given is a string S of length N.  Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.  More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:    * l_1 + len \leq l_2    * S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)  If there is no such integer len, print 0.

Submission attempts (in order):
N = int(input())
S = input()

def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1
    j = 0
    l = len(S)
    A[0] = l

    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        if i <= j:
             A[i] = j
        else:
             A[i] = 0
        k = 1
        while min(l-i, j-A[k]) > k:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

ans = 0
for i in range(N):
    s = S[i:]
    tmp = z_algo(s)
    if len(tmp) >= 2:
        ans = max(ans, max(tmp[1:]))
print(ans)



N = int(input())
S = input()

def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1
    j = 0
    l = len(S)
    A[0] = l

    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 min(l-i, j-A[k]) > k:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

ans = 0
for i in range(N):
    s = S[i:]
    tmp = z_algo(s)
    
    for i in range(1, len(tmp)):
        if tmp[i] > i:
            tmp[i] = 0
    if len(tmp) >= 2:
        ans = max(ans, max(tmp))
print(ans)

N = int(input())
S = input()

for width in range(N, 0, -1):
    flg = False
    for l in range(N):
        r = l + width
        if r > N:
            break
        s = S[l:r]
        for i in range(l+1, N-width+1):
            if s == S[i:i+width]:
                print(width)
                exit()
print(0)

N = int(input())
S = input()
m = 0
for width in range(1, N):
    flg = False
    for l in range(N):
        r = l + width
        if r > N:
            break
        s = S[l:r]
        for i in range(l+1, N-width+1):
            if s == S[i:i+width]:
                m = width
                flg = True
                break
        if flg:
          break
print(m)

N = int(input())
S = input()

def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1
    j = 0
    l = len(S)
    A[0] = l

    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        k = 1
        while min(l-i, j-A[k]) > k:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

ans = 0
for i in range(N):
    s = S[i:]
    tmp = z_algo(s)
    for i in range(1, len(tmp)+1):
        if tmp[i] > i:
            tmp[i] = 0
    if len(tmp) >= 2:
        ans = max(ans, max(tmp[1:]))
print(ans)



n = int(input())
s = input()
 
max_len = 0
j = 0
 
for i in range(n):
    while j < n and s[i:j] in s[j:]:
        j += 1
    max_len = max(max_len,j-i-1)
 
print(max_len)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted