Problem:


Score : 500 points

Problem Statement

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.

Constraints

2 \leq N \leq 5 \times 10^3

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5



Sample input:
5
ababa


Sample output:
2

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from random import randint

def main():
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())   
    
    class RollingHash:
        def __init__(self, s):
            self.s = s    # パターン探索を行われる側の文字列
            self.size = len(s)
            self.hashmod = 2 ** 32 - 1
            self.base = randint(2 ** 10, 2 ** 11)
            self.hash_list = self._make_rolling_hash_list()

        def _make_rolling_hash_list(self):
            hash_list = [0] * (self.size + 1)    # [h(s[0:0]), h(s[0:1]), h(s[0:2]), h(s[0:3]), ..., h(s[0:size])]
            prev = 0
            for i in range(1, self.size + 1):
                hash_num = (prev * self.base + ord(self.s[i-1])) % self.hashmod
                hash_list[i] = hash_num
                prev = hash_num
            return hash_list
        
        def calc_interval_hash(self, i, j):
            """
            s[i:j] のハッシュを計算
            """
            return (self.hash_list[j] - self.hash_list[i] * pow(self.base, (j - i), self.hashmod)) % self.hashmod

    
    def span_bisect(rolling):
        left = 1    # 絶対に発見成功するとわかっている
        right = (n // 2) + 1    # 絶対に発見失敗するとわかっている
        while right - left > 1:
            d = dict()
            mid_span = left + (right - left) // 2    # 二分探索で今回選ばれた span
            for i in range(n - mid_span + 1):
                j = i + mid_span
                h = rolling.calc_interval_hash(i, j)
                try:
                    first_j = d[h]
                    if first_j <= i:
                        left = mid_span
                        break
                except KeyError:
                    d[h] = j
            else:
                right = mid_span
        return left


    n = ii()
    s = input()
    if len(list(set(list(s)))) == n:
        print(0)
    else:
        # main
        rolling = RollingHash(s)
        print(span_bisect(rolling))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    from random import randint
    class RollingHash:
        def __init__(self, s):
            self.s = s    # パターン探索を行われる側の文字列
            self.size = len(s)
            self.hashmod = 2 ** 64 - 1
            self.base = randint(2 ** 10, 2 ** 11)
            self.hash_list = self._make_rolling_hash_list()

        def _make_rolling_hash_list(self):
            hash_list = [0] * (self.size + 1)    # [h(s[0:0]), h(s[0:1]), h(s[0:2]), h(s[0:3]), ..., h(s[0:size])]
            prev = 0
            for i in range(1, self.size + 1):
                hash_num = (prev * self.base + ord(self.s[i-1])) % self.hashmod
                hash_list[i] = hash_num
                prev = hash_num
            return hash_list

        def calc_hash_str(self, pattern):
            """
            pattern のハッシュを計算
            """
            n = len(pattern)
            hash_num = 0
            for i in range(n):
                hash_num = (hash_num + ord(pattern[i]) * pow(self.base, n-1-i, self.hashmod)) % self.hashmod
            return hash_num
        
        def calc_interval_hash(self, i, j):
            """
            s[i:j] のハッシュを計算
            """
            return (self.hash_list[j] - self.hash_list[i] * (self.base ** (j - i))) % self.hashmod

    
    n = ii()
    s = input()
    if len(list(set(list(s)))) == n:
        print(0)
    else:
        # main
        rolling = RollingHash(s)
        updated = True
        ans = 0
        for span in range(1, (n // 2) + 1):
            if not updated:
                break
            updated = False
            d = dict()
            for i in range(n - span + 1):
                j = i + span
                h = rolling.calc_interval_hash(i, j)
                try:
                    right = d[h]
                    if right <= i:
                        ans = span
                        updated = True
                except KeyError:
                    d[h] = j
                if updated:
                    break
        print(ans)

Submission status: Wrong Answer

Solution code:
def rolling_hash(s):
    l = len(s)
    h = [0]*(l + 1)
    v = 0
    for i in range(l):
        h[i+1] = v = (v * base + ord(s[i])) % mod
    return h
def setup_pw(l):
    pw = [1]*(l + 1)
    v = 1
    for i in range(l):
        pw[i+1] = v = v * base % mod
    return pw
def get(h, b, l, r):
    return (h[r] - h[l] * b[r-l]) % mod

def check_bisect(l,h,b):
    for j in range(N-2*l+1):
        for k in range(N-2*l-j+1):
            if get(h,b,j,l+j) == get(h,b,j+l+k,2*l+j+k):
                return True
    return False

N = int(input())
S = input()
mod = 1000000007
base = 401
h = rolling_hash(S)
b = setup_pw(N)
left = 1
right = N // 2
finish = False
if N <= 26:
    s = set(list(S))
    if N <= 3:
        if len(s) < N:
            print('1')
        else:
            print('0')
        finish = True
    else:
        if len(s) == N:
            print('0')
            finish = True
while finish == False:
    if left == right:
        print(left)
        finish = True
    elif right - left == 1:
        if check_bisect(right,h,b):
            print(right)
        else:
            print(left)
        finish = True
    else:
        mid = (left+right) // 2
        if check_bisect(mid,h,b):
            left = mid
        else:
            right = mid - 1

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
l = 0
r = n
while r - l > 1:
    m = l + (r-l)//2
    hash = set()
    flag = 0
    for i in range(n-m+1):
        h = rolling_hash(i, i+m)
        if h not in hash:
            hash.add(h)
        elif i >= m:
            flag = 1
            break
    if flag == 1:
        l = m
    else:
        r = m

print(l)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
l = -1
r = n
while r - l > 1:
    m = l + (r-l)//2
    hash = set()
    flag = 0
    for i in range(n-m+1):
        h = rolling_hash(i, i+m)
        if h not in hash:
            hash.add(h)
        elif i >= m:
            flag = 1
            break
    if flag == 1:
        l = m
    else:
        r = m

print(min(n//2, l))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
l = -1
r = n
while r - l > 1:
    m = l + (r-l)//2
    hash = set()
    flag = 0
    for i in range(n-m+1):
        h = rolling_hash(i, i+m)
        if h not in hash:
            hash.add(h)
        else:
            flag = 1
            break
    if flag == 1:
        l = m
    else:
        r = m

print(min(n//2, l))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
ans = 0
l = 0
r = n
while r - l > 1:
    m = l + (r-l)//2
    hash = set()
    flag = 0
    for i in range(n-m+1):
        h = rolling_hash(i, i+m)
        if h not in hash:
            hash.add(h)
        else:
            flag = 1
            break
    if flag == 1:
        l = m
    else:
        r = m

print(min(n//2, l))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
ans = 0
l = 0
r = n
while r - l > 1:
    m = l + (r-l)//2
    hash = set()
    flag = 0
    for i in range(n-m+1):
        h = rolling_hash(i, i+m)
        if h not in hash:
            hash.add(h)
        else:
            flag = 1
            break
    if flag == 1:
        l = m
    else:
        r = m

print(l)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
N=int(sys.stdin.readline().strip())
S=sys.stdin.readline().strip()

ans=float("-inf")
for i in range(N):
    j_start_idx=None
    length=0
    while j_start_idx is None or i<j_start_idx:
        s=S[i]
        for j in range(i+1,N):
            if s==S[j]:
                if j_start_idx is None: #後ろに現れる文字列の先頭の位置を記録
                    j_start_idx=j
                length+=1
                i+=1
                break
        else:
            break
    ans=max(ans,length)

print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
ans = 0
for i in range(n//2, 0, -1):#length
    for j in range(n-i+1):
        h = rolling_hash(j, j+i)
        if h not in hash:
            hash.add(h)
        else:
            ans = i
            break
    if ans != 0:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    s = input()
    x = 1

    sn = [ord(c) - 96 + x for c in s]

    lmax = n // 2
    m1 = 10 ** 9 + 7
    m2 = 2 ** 61 - 1

    ans = 0

    for l in range(lmax, 0, -1):
        appeared = set()
        diff = pow(m1, l - 1, m2)

        h_init = 0
        for i in range(l):
            h_init += sn[i] * pow(m1, l - 1 - i, m2)

        hashlist = [0] * n
        hashlist[0] = h_init
        h = h_init

        for i in range(l, n):
            h -= sn[i - l] * diff
            h *= m1
            h += sn[i]
            h %= m2
            hashlist[i] = h
            if i - l >= 0:
                appeared.add(hashlist[i - l])
            if h in appeared:
                print(l)
                sys.exit()

    print(0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

import random
base1 = 1007
mod1 = 10**9+7
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
base1 = 1007
mod1 = 10**9+7
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
def rolling_hash(i, j):
    return (hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1

hash = set()
ans = 0
for i in range(n//2, 0, -1):#length
    for j in range(n-i+1):
        h = rolling_hash(j, j+i)
        if h not in hash:
            hash.add(h)
        else:
            ans = i
            break
    if ans != 0:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def main():
    n = int(input())
    s = list(input())
    for i in range(n):
        s[i] = ord(s[i]) - ord("a")
    s = np.array(s)
    dp = np.zeros((n+1, n+1), dtype=np.int64)
    for i in range(n):
        c = s[i]
        equal = (c == s)
#        print(equal)
        dp[i+1][1:] += equal
        dp[i+1][1:] += equal * dp[i][:-1]
#    for i in range(n+1):
#        print(dp[i])
    ans = 0
    for i in range(1, n):
        tmp = dp[i][i+1:]
        tmp = tmp[ans<tmp]
        if 0 < len(tmp):
            tmp = tmp[tmp<=n//2]
        if 0 < len(tmp):
            ans = max(ans, max(tmp))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
Z = [0 for _ in range(n)]
ans = 0
for begin in range(n):
    Z[begin] = n-begin
    i = begin + 1
    j = 0
    while i < n:
        while i+j < n and s[begin+j] ==s[i+j]:
            j += 1
        Z[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while k < j and Z[begin+k] < j:
            Z[i+k] = Z[begin+k]
            k += 1
        i += k
        j -= k
    for i in range(begin,n):
        l = min(i-begin,Z[i])
        ans = max(ans,l)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
Z = [0 for _ in range(n)]
ans = 0
for begin in range(n):
    Z[begin] = n-begin
    i = begin + 1
    j = 0
    while i < n:
        while i+j < n and s[begin+j] ==s[i+j]:
            j += 1
        Z[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while k < j and Z[k] < j:
            Z[i+k] = Z[k]
            k += 1
        i += k
        j -= k
    for i in range(begin,n):
        l = min(i-begin,Z[i])
        ans = max(ans,l)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()

from collections import defaultdict

def check(l):
    d = defaultdict(lambda: -1)
    for i in range(n-l+1):
        if d[s[i:i+l]] != -1:
            if d[s[i:i+l]] + l <= i:
                return True
        d[s[i:i+l]]=i

    return False

ok=0
ng=len(s)

while abs(ok-ng)>1:
    mid=(ok+ng)//2

    if check(mid):
        ok=mid
    else:
        ng=mid

print(ok)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()

from collections import defaultdict

def check(l):
    d = defaultdict(lambda: -1)
    for i in range(n-l+1):
        if d[s[i:i+l]] != -1:
            if d[s[i:i+l]] + l <= i:
                return True
        d[s[i:i+l]]=i

    return False

ok=0
ng=1+len(s)//2

while abs(ok-ng)>1:
    mid=(ok+ng)//2

    if check(mid):
        ok=mid
    else:
        ng=mid

print(ok)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()

def solve(SS):
	n = len(SS)
	Z = [0] * n
	Z[0] = n
	i, j = 1, 0
	while i < n:
		while (i + j < n) and (SS[j] == SS[i + j]):
			j += 1
		Z[i] = j
		if j == 0:
			i += 1
			continue
		else:
			k = 1
			while (i + k < n) and (k + Z[k] < j):
				Z[i + k] = Z[k]
				k += 1
			i += k
			j -= k

	out = 0
	for i in range(n):
		tmp = min(Z[i], i + 1)
		out = max(out, tmp)
	return out

ans = 0
for i in range(N):
	ans = max(ans, solve(S[i:]))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()

def solve(SS):
	n = len(SS)
	Z = [0] * n
	i, j = 1, 0
	while i < n:
		while (i + j < n) and (j < i) and (SS[j] == SS[i + j]):
			j += 1
		Z[i] = j
		if j == 0:
			i += 1
			continue
		else:
			k = 1
			while (i + k < n) and (k + Z[k] < j):
				Z[i + k] = Z[k]
				k += 1
			i += k
			j -= k

	return max(Z)

ans = 0
for i in range(N):
	ans = max(ans, solve(S[i:]))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(1,(N//2) + 1):
	tmp = []
	for j in range(N):
		if j + i - 1>= N:
			break
		s = S[j:j+i]
		if s in tmp:
			ans = i
			break
		else:
			tmp.append(s)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=input()
Q=[]
for i in range(n):
    Q.append(S[i:])
Q.sort()
#print(Q)
ans=0
nowS=Q[0]
nowL=len(nowS)
for i in range(n-1):
    chkS=Q[i+1]
    chkL=len(chkS)
    cnt=0
    for j in range(min(nowL,chkL)):
        if nowS[j]==chkS[j]:
            cnt+=1
        else:
            break
    #print(nowS,chkS,cnt)
    ans=max(min(cnt,abs(nowL-chkL)),ans)
    nowS=chkS
    nowL=chkL
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=input()
Q=[]
m=n//2
for i in range(n):
    Q.append([S[i:min(n,i+m)],i])
Q.sort()
#print(Q)
ans=0
nowS=Q[0][0]
nowL=Q[0][1]
for i in range(n-1):
    chkS=Q[i+1][0]
    chkL=Q[i+1][1]
    cnt=0
    for j in range(min(len(nowS),len(chkS))):
        if nowS[j]==chkS[j]:
            cnt+=1
        else:
            break
    #print(nowS,chkS,cnt)
    ans=max(min(cnt,abs(nowL-chkL)),ans)
    nowS=chkS
    nowL=chkL
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000)


def DFS(steds:list,S:str,N:int):


    if len(steds)==1:
    #    print(steds,"!")
        return 0
    
    steds.sort()
    for A,B in zip(steds,steds[1:]):
        if A[1] < B[0]:
            break
    else:
    #    print(steds,"?")
        return 0

    last = dict()

    for st,ed in steds:
        if ed == N-1:
            continue
        if not S[ed+1] in last:
            last[S[ed+1]]=[]
        last[S[ed+1]].append([st,ed+1])

    ans = steds[0][1]-steds[0][0]+1
    #print(ans,steds)
    for nsteds in last:
        ans = max(DFS(last[nsteds],S,N),ans)
    
    return ans




def resolve():
    N = int(input())
    S = input()

    kasiras = dict()
    for i,v in enumerate(S):
        if not v in kasiras:
            kasiras[v]=[]
        kasiras[v].append([i,i])
    mx =0
    for i in kasiras:
        mx = max(mx,DFS(kasiras[i],S,N))
    print(mx)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
ans=0
for x in range(N-1):
    S=s[x:]
    l=len(S)
    A=[0]*l
    A[0]=l
    i=x+1;j=0
    while(i<l):
        while(i+j<l and S[j]==S[i+j]):
            j+=1
        A[i]=j
        if(j==0):
            i+=1
            continue
        k=1
        while(i+k<l and k+A[k]<j):
            A[i+k]=A[k]
            k+=1
        i+=k
        j-=k
    m=0
    for z in range(l):
        if(z-A[z]>=0):
            m=max(m,A[z])
    ans=max(m,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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] and i > j:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

ans = 0
for index in range(n-1):
	res = z_algo(s[index:])
	tmp = max(res[1:])
	ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for index in range(n-1):
	res = z_algo(s[index:])
	tmp = max(res[1:])
	ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)
	if ans > (n-i):
		break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)
	if ans > (n-i) // 2+1:
		break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)
	if ans > (n-i) // 2 -1:
		break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	tmp = max(res[1:])
	ans = max(ans, tmp)
	if ans > (n-i) // 2 :
		break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys #追加

n = int(input())
s = input()

sys.setrecursionlimit(10**9) #追加

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
        if A[i] >= N - i - 1:
        	return A[i]
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return 0

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	ans = max(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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
        if A[i] >= N - i - 1:
        	return A[i]
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return 0

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	ans = max(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

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
        if A[i] >= N - i - 1:
        	return A[i]
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return 0

ans = 0
for i in range(n-1):
	res = z_algo(s[i:])
	ans = max(ans, res)
	if ans > (n-i) // 2:
		break

print(ans)