Submission #684198


Source Code Expand

n = input()
s = raw_input()

# SA-IS (O(nlogn))
def SuffixArray(s):
    m = map(ord, s)
    m.append(-1)
    n = len(m)

    def i_large(t, SA, s, bktl):
        bkt = {}
        for k in bktl: bkt[k] = bktl[k]
        for e in SA:
            j = e-1
            if j>=0 and not t[j]:
                SA[bkt[s[j]]] = j
                bkt[s[j]] += 1

    def i_small(t, SA, s, bkts):
        bkt = {}
        for k in bkts: bkt[k] = bkts[k]
        for e in reversed(SA):
            j = e-1
            if j>=0 and t[j]:
                bkt[s[j]] -= 1
                SA[bkt[s[j]]] = j

    def SAIS(s, n):
        t = [0]*n; LMS = [0]*n
        SA = [0]*n

        t[-2] = 0; t[-1] = 1
        for i in xrange(n-3, -1, -1):
            t[i] = 1 if s[i]<s[i+1] or (s[i]==s[i+1] and t[i+1]) else 0
        for i in xrange(1, n):
            LMS[i] = 1 if t[i] and not t[i-1] else 0

        su = 0
        bkt = {}
        bktl = {}; bkts = {}
        for c in s: bkt[c] = bkt.get(c, 0) + 1
        for k in sorted(bkt):
            bktl[k] = su
            su += bkt[k]
            bkts[k] = su

        for k in bkts: bkt[k] = bkts[k]
        for i in xrange(n): SA[i] = -1
        for i in xrange(1, n):
            if LMS[i]:
                bkt[s[i]] -= 1
                SA[bkt[s[i]]] = i

        i_large(t, SA, s, bktl)
        i_small(t, SA, s, bkts)

        n1 = 0
        for e in SA:
            if e>0 and LMS[e]:
                SA[n1] = e
                n1 += 1

        for i in xrange(n1, n): SA[i] = -1
        cnt = 0; prev = -1
        for i in xrange(n1):
            pos = SA[i]
            for d in xrange(n):
                if prev==-1 or s[pos+d]!=s[prev+d] or t[pos+d]!=t[prev+d]:
                    cnt += 1
                    prev = pos
                    break
                elif LMS[pos+d] or LMS[prev+d]: break
            pos /= 2
            SA[n1+pos] = cnt-1
        j = n-1
        for i in xrange(n-1, n1-1, -1):
            if SA[i]>=0:
                SA[j] = SA[i]
                j -= 1

        if cnt<n1:
            SA[:n1] = SAIS(SA[-n1:], n1)
        else:
            for i in xrange(n1): SA[SA[i-n1]] = i

        for k in bkts: bkt[k] = bkts[k]
        j = 0
        for i in xrange(1, n):
            if LMS[i]:
                SA[j-n1] = i
                j += 1
        for i in xrange(n1): SA[i] = SA[SA[i]-n1]
        for i in xrange(n1, n): SA[i] = -1

        for i in xrange(n1-1, -1, -1):
            j = SA[i]; SA[i] = -1
            bkt[s[j]] -= 1
            SA[bkt[s[j]]] = j

        i_large(t, SA, s, bktl)
        i_small(t, SA, s, bkts)
        return SA
    return SAIS(m, n)

rank = [0]*(n+1)
def LCP(s, n, sa):
    lcp = [-1]*(n+1)
    #rank = [0]*(n+1)
    for i in xrange(n+1): rank[sa[i]] = i

    h = 0
    lcp[0] = 0
    for i in xrange(n):
        j = sa[rank[i] - 1]
        if h > 0: h -= 1
        while j+h < n and i+h < n and s[j+h]==s[i+h]:
            h += 1
        lcp[rank[i] - 1] = h
    return lcp

sa = SuffixArray(s)
lcp = LCP(s, n, sa)

# LCP StarrySkyTree
class LCPSST:
    def __init__(self, sa, lcp):
        n0 = len(lcp)
        n = 1
        while n < n0:
            n *= 2
        self.n = n
        self.data = data = [10**9]*(2*n)
        self.sa = sa
        for i in xrange(n0):
            data[i+n-1] = lcp[i]
        for i in xrange(n-2, -1, -1):
            data[i] = min(data[2*i+1], data[2*i+2])
    def __query(self, a, b, k, l, r):
        if r <= a or b <= l:
            return 10**9
        if a <= l and r <= b:
            return self.data[k]
        else:
            vl = self.__query(a, b, 2*k+1, l, (l + r) / 2)
            vr = self.__query(a, b, 2*k+2, (l + r) / 2, r)
            return min(vl, vr)
    def __call__(self, i, j):
        sa = self.sa
        a = rank[i]; b = rank[j]
        #print rank[i], rank[j]
        if a > b:
            a,b = b,a
        return self.__query(a, b, 0, 0, self.n)

lcpq = LCPSST(sa, lcp)

# i < j
# XXXXXX YYY
# YYY XXXXXX
def comp(i, j):
    res = 1
    if i > j:
        i,j = j,i
        res = -1
    r = lcpq(i, j)
    #print i, j, r, n-(j-i)
    if r < n-j:
        return cmp(rank[i], rank[j])*res
    r = lcpq(n+i-j+1, i)
    #print n+i-j+1, i, r
    if r < j-i:
        return cmp(rank[n+i-j+1], rank[i])*res
    return 0

a = range(0, n)
a.sort(cmp=comp)

print "\n".join(map(str, map(lambda x: x+1, a)))

Submission Info

Submission Time
Task D - Suffix Concat
User yaketake08
Language PyPy2 (5.6.0)
Score 0
Code Size 4562 Byte
Status TLE
Exec Time 4214 ms
Memory 82844 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 11
TLE × 46
Set Name Test Cases
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt
Case Name Status Exec Time Memory
01.txt AC 72 ms 9200 KB
02.txt AC 81 ms 9200 KB
03.txt AC 71 ms 9200 KB
04.txt AC 81 ms 9200 KB
05.txt AC 65 ms 9200 KB
06.txt AC 1684 ms 53532 KB
07.txt AC 1606 ms 49692 KB
08.txt AC 1015 ms 45724 KB
09.txt AC 1156 ms 45596 KB
10.txt TLE 4212 ms 66716 KB
11.txt TLE 4211 ms 64412 KB
12.txt TLE 4212 ms 66460 KB
13.txt TLE 4213 ms 72988 KB
14.txt AC 1520 ms 46748 KB
15.txt AC 1613 ms 50460 KB
16.txt TLE 4212 ms 64796 KB
17.txt TLE 4213 ms 75292 KB
18.txt TLE 4213 ms 73500 KB
19.txt TLE 4213 ms 74524 KB
20.txt TLE 4213 ms 69020 KB
21.txt TLE 4213 ms 67740 KB
22.txt TLE 4213 ms 68380 KB
23.txt TLE 4213 ms 71068 KB
24.txt TLE 4213 ms 66972 KB
25.txt TLE 4214 ms 74780 KB
26.txt TLE 4213 ms 68636 KB
27.txt TLE 4213 ms 67868 KB
28.txt TLE 4212 ms 70300 KB
29.txt TLE 4213 ms 74140 KB
30.txt TLE 4212 ms 63132 KB
31.txt TLE 4213 ms 72860 KB
32.txt TLE 4212 ms 68764 KB
33.txt TLE 4212 ms 67740 KB
34.txt TLE 4213 ms 67484 KB
35.txt TLE 4213 ms 68508 KB
36.txt TLE 4213 ms 68892 KB
37.txt TLE 4213 ms 74780 KB
38.txt TLE 4214 ms 75676 KB
39.txt TLE 4213 ms 67612 KB
40.txt TLE 4213 ms 69660 KB
41.txt TLE 4213 ms 74524 KB
42.txt TLE 4214 ms 80028 KB
43.txt TLE 4213 ms 69276 KB
44.txt TLE 4213 ms 69148 KB
45.txt TLE 4214 ms 80412 KB
46.txt TLE 4213 ms 68636 KB
47.txt TLE 4214 ms 76316 KB
48.txt TLE 4214 ms 82844 KB
49.txt TLE 4213 ms 68508 KB
50.txt TLE 4213 ms 72092 KB
51.txt TLE 4212 ms 65308 KB
52.txt TLE 4213 ms 67612 KB
53.txt TLE 4212 ms 63132 KB
54.txt TLE 4213 ms 71708 KB
55.txt TLE 4212 ms 65564 KB
56.txt TLE 4213 ms 69020 KB
57.txt TLE 4214 ms 76188 KB