結果

問題 No.430 文字列検索
ユーザー simansiman
提出日時 2019-12-16 16:57:06
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 631 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 11,596 KB
実行使用メモリ 35,860 KB
最終ジャッジ日時 2023-09-15 18:02:07
合計ジャッジ時間 3,702 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
19,560 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class String
  def contain_points(str)
    l1 = size
    l2 = str.size

    return [] if l1 < l2

    base = 10 ** 9 + 7
    t = 1
    l2.times { t *= base }
    cp = codepoints

    h1 = 0
    h2 = 0

    l2.times do |i|
      h1 = h1 * base + cp[i]
      h2 = h2 * base + str[i].ord
    end

    i = 0
    result = []

    while i + l2 <= l1
      result << i if h1 == h2

      if i + l2 < l1
        h1 = h1 * base + cp[i + l2] - cp[i] * t
      end

      i += 1
    end

    result
  end
end

S = gets.chomp
M = gets.to_i

ans = 0

M.times do
  str = gets.chomp

  res = S.contain_points(str)
  ans += res.size
end

puts ans
0