結果

問題 No.515 典型LCP
ユーザー Min_25Min_25
提出日時 2017-05-06 12:49:01
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 1,084 bytes
コンパイル時間 1,730 ms
コンパイル使用メモリ 77,572 KB
実行使用メモリ 99,428 KB
最終ジャッジ日時 2023-10-12 15:01:13
合計ジャッジ時間 7,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 665 ms
99,428 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 72 ms
76,396 KB
testcase_04 AC 72 ms
76,308 KB
testcase_05 AC 288 ms
79,796 KB
testcase_06 AC 296 ms
79,788 KB
testcase_07 AC 280 ms
80,048 KB
testcase_08 AC 373 ms
80,160 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 244 ms
79,540 KB
testcase_16 AC 269 ms
80,072 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]]
    for j in range(min(len(s1), len(s2))):
      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