結果

問題 No.515 典型LCP
ユーザー rpy3cpprpy3cpp
提出日時 2017-05-06 01:49:52
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 887 ms / 1,000 ms
コード長 2,465 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 77,672 KB
実行使用メモリ 156,568 KB
最終ジャッジ日時 2023-10-12 11:21:44
合計ジャッジ時間 7,926 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 887 ms
156,568 KB
testcase_01 AC 784 ms
155,940 KB
testcase_02 AC 337 ms
81,852 KB
testcase_03 AC 75 ms
76,548 KB
testcase_04 AC 74 ms
76,180 KB
testcase_05 AC 233 ms
84,664 KB
testcase_06 AC 248 ms
84,600 KB
testcase_07 AC 238 ms
84,648 KB
testcase_08 AC 309 ms
84,620 KB
testcase_09 AC 237 ms
81,088 KB
testcase_10 AC 273 ms
83,588 KB
testcase_11 AC 276 ms
83,400 KB
testcase_12 AC 275 ms
83,468 KB
testcase_13 AC 248 ms
85,900 KB
testcase_14 AC 166 ms
92,056 KB
testcase_15 AC 260 ms
112,024 KB
testcase_16 AC 273 ms
112,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

class RMQ(object):
    def __init__(self, A):
        self._A = A
        self._preprocess()

    def _preprocess(self):
        n = len(self._A)
        max_j = n.bit_length() - 1
        self._M = [range(n)]
        for j in range(0, max_j):
            shift = 1 << j
            Mj = self._M[j]
            Mjnext = []
            for k1, k2 in zip(Mj, Mj[shift:]):
                k = k1 if self._A[k1] < self._A[k2] else k2
                Mjnext.append(k)
            self._M.append(Mjnext)

    def query(self, i, j):
        if i == j: return i
        if i > j: i, j = j, i
        el = (j - i).bit_length() - 1
        k1 = self._M[el][i]
        k2 = self._M[el][j - (1 << el) + 1]
        rmq = k1 if self._A[k1] < self._A[k2] else k2
        return rmq

class LCP(object):
    def __init__(self, strings):
        strings_with_index = [(s, i) for i, s in enumerate(strings)]
        strings_with_index.sort()
        sorted_strings = []
        self._index_converter = [0] * len(strings)
        j = 0
        for s, i in strings_with_index:
            sorted_strings.append(s)
            self._index_converter[i] = j
            j += 1
        self._init_lcp_neighbours(sorted_strings)
        self._rmq = RMQ(self._lcp_neighbours)
        self._lens = list(map(len, sorted_strings))
            
    def _init_lcp_neighbours(self, ss):
        n = len(ss)
        self._lcp_neighbours = [self._calc_lcp(ss[i], ss[i + 1]) for i in range(n - 1)]

    def _calc_lcp(self, str0, str1):
        lcp = 0
        for c0, c1 in zip(str0, str1):
            if c0 != c1:
                return lcp
            else:
                lcp += 1
        return lcp
    
    def get(self, i, j):
        ''' strings[i] と strings[j] の LCP の長さを返す
        '''
        ii = self._index_converter[i]
        jj = self._index_converter[j]
        if ii == jj:
            return self._lens[ii]
        if ii > jj: ii, jj = jj, ii
        kk = self._rmq.query(ii, jj - 1)
        return self._lcp_neighbours[kk]


if __name__ == '__main__':
    N = int(raw_input())
    strings = [raw_input() for _ in range(N)]
    M, x, d = map(int, raw_input().split())
    lcp = LCP(strings)
    Nm1 = N - 1
    NN = N * (N - 1)
    cum = 0
    for k in xrange(M):
        i, j = divmod(x, Nm1)
        if (i > j):
            i, j = j, i
        else:
            j += 1
        cum += lcp.get(i, j)
        x = (x + d) % NN
    print(cum)
0