結果

問題 No.515 典型LCP
ユーザー Min_25Min_25
提出日時 2017-05-06 12:52:56
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 748 ms / 1,000 ms
コード長 1,103 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 77,608 KB
実行使用メモリ 99,516 KB
最終ジャッジ日時 2023-10-12 15:01:22
合計ジャッジ時間 8,208 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 748 ms
99,516 KB
testcase_01 AC 647 ms
99,304 KB
testcase_02 AC 392 ms
80,944 KB
testcase_03 AC 75 ms
76,416 KB
testcase_04 AC 74 ms
76,496 KB
testcase_05 AC 288 ms
79,848 KB
testcase_06 AC 303 ms
80,004 KB
testcase_07 AC 291 ms
80,080 KB
testcase_08 AC 376 ms
80,260 KB
testcase_09 AC 274 ms
80,052 KB
testcase_10 AC 300 ms
81,152 KB
testcase_11 AC 283 ms
80,300 KB
testcase_12 AC 282 ms
80,548 KB
testcase_13 AC 263 ms
79,160 KB
testcase_14 AC 111 ms
79,424 KB
testcase_15 AC 250 ms
79,264 KB
testcase_16 AC 280 ms
79,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def solve():
  def ilog2(n):
    return 0 if n <= 0 else n.bit_length() - 1

  input = sys.stdin.readline

  N = int(input())
  strs = [input().rstrip() for _ in range(N)]
  M, x, d = map(int, input().split())

  perm = [i for i in range(N)]
  perm.sort(key=lambda i: strs[i])
  iperm = [0] * N
  for i, p in enumerate(perm):
    iperm[p] = i

  size = N - 1
  lcp = [0] * size
  for i in range(size):
    s1, s2 = strs[perm[i]], strs[perm[i + 1]]
    lcp[i] = l = min(len(s1), len(s2))
    for j in range(l):
      if s1[j] != s2[j]:
        lcp[i] = j
        break

  lgN = ilog2(size)
  tab = [lcp]
  for k in range(1, lgN + 1):
    s = 1 << (k - 1)
    prev, curr = tab[-1], [0] * (size + 1 - 2 * s)
    for i in range(len(curr)):
      curr[i] = min(prev[i], prev[i + s])
    tab += [curr]

  ans = 0
  mod = N * (N - 1)
  for _ in range(M):
    i, j = divmod(x, N - 1)
    j += (i <= j)
    i, j = iperm[i], iperm[j]
    if i > j:
      i, j = j, i
    l = ilog2(j - i)
    ans += min(tab[l][i], tab[l][j - (1 << l)])
    x += d
    if x >= mod:
      x -= mod

  print(ans)

solve()
0