結果

問題 No.430 文字列検索
ユーザー むらためむらため
提出日時 2019-10-01 12:05:53
言語 Nim
(2.0.2)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 2,479 bytes
コンパイル時間 3,633 ms
コンパイル使用メモリ 62,964 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 08:32:19
合計ジャッジ時間 5,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 57 ms
6,940 KB
testcase_02 AC 61 ms
6,944 KB
testcase_03 AC 72 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 46 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 78 ms
6,940 KB
testcase_12 AC 77 ms
6,944 KB
testcase_13 AC 75 ms
6,940 KB
testcase_14 AC 77 ms
6,944 KB
testcase_15 AC 81 ms
6,940 KB
testcase_16 AC 97 ms
6,940 KB
testcase_17 AC 101 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,algorithm
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" ,discardable.}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

type SuffixArray = ref object
  S : string
  n : int
  SA : seq[int]
  LCP : seq[int]
proc newSuffixArray(S:string):SuffixArray =
  new(result)
  result.S = S
  let n = S.len
  result.n = n
  # SA
  var G = newSeq[int](n+1)
  for i in 0..<n: G[i] = S[i].int
  G[n] = -1
  proc SAComp(h:int):proc(x,y:int):int =
    return proc(x,y:int):int =
      if x == y : return 0
      if G[x] == G[y] : return G[x+h] - G[y+h]
      return G[x] - G[y]
  # ここのソートに N*logN*logN かかる
  result.SA = toSeq(0..n)
  result.SA.sort(0.SAComp)
  var B = newSeq[int](n+1)
  var h = 1
  while B[n] != n:
    let comp = h.SAComp
    result.SA.sort(comp)
    for i in 0..<n:
      B[i+1] = B[i] + (if comp(result.SA[i],result.SA[i+1]) < 0 : 1 else: 0)
    for i in 0..n: G[result.SA[i]] = B[i]
    h *= 2
  # LCP (最長共通接頭辞)
  # SA格納順に隣同士の最長共通接頭辞の長さ
  # kasai's algorithm で O(N) にできる
  result.LCP = newSeq[int](n+1)
  h = 0
  for i in 0..n: B[result.SA[i]] = i
  for i in 0..n:
    if B[i] > 0 :
      var j = result.SA[B[i]-1]
      while j + h < n and i + h < n and S[j+h] == S[i+h]: h += 1
      result.LCP[B[i]] = h
    if h > 0 : h -= 1
  result.LCP[0] = -1
iterator find(self:SuffixArray,target:string): int =
  var a = -1
  var b = self.n + 1
  while a + 1 < b:
    let m = (a + b) shr 1
    let start = self.SA[m]
    if cmp(self.S[start..<self.S.len.min(start+target.len)],target) < 0 : a = m
    else : b = m
  var already = false
  while b <= self.n :
    let start = self.SA[b]
    if already: # 既にマッチしているので更にもっとマッチするかどうか見るだけで良い
      if self.LCP[b] >= target.len: yield start
      else: break
    elif cmp(self.S[start..<self.S.len.min(start+target.len)],target) == 0 :
      yield start
      already = true
    else: break
    b += 1
proc getCount(self:SuffixArray,target:string): int =
  for _ in self.find(target): result += 1

let S = stdin.readLine()
let SA = S.newSuffixArray()
var ans = 0
scan().times:
  let T = stdin.readLine()
  ans += SA.getCount(T)
echo ans
0