結果

問題 No.430 文字列検索
ユーザー HaarHaar
提出日時 2018-10-11 20:46:56
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,566 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 10,300 ms
コンパイル使用メモリ 264,068 KB
実行使用メモリ 93,636 KB
最終ジャッジ日時 2023-09-13 20:25:04
合計ジャッジ時間 35,461 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,017 ms
63,852 KB
testcase_01 AC 1,566 ms
93,636 KB
testcase_02 AC 1,374 ms
67,004 KB
testcase_03 AC 1,400 ms
67,108 KB
testcase_04 AC 1,029 ms
63,732 KB
testcase_05 AC 1,023 ms
63,956 KB
testcase_06 AC 1,008 ms
63,952 KB
testcase_07 AC 1,021 ms
63,784 KB
testcase_08 AC 1,435 ms
93,224 KB
testcase_09 AC 1,041 ms
64,008 KB
testcase_10 AC 1,147 ms
64,168 KB
testcase_11 AC 1,490 ms
69,652 KB
testcase_12 AC 1,486 ms
69,288 KB
testcase_13 AC 1,483 ms
69,472 KB
testcase_14 AC 1,516 ms
69,352 KB
testcase_15 AC 1,501 ms
67,152 KB
testcase_16 AC 1,476 ms
67,352 KB
testcase_17 AC 1,447 ms
67,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.math._
import scala.collection.mutable.{HashMap => MHashMap, _}
import scala.collection.immutable._

object Main{
  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)
    val pw = new java.io.PrintWriter(System.out)

    val s = sc.next
    val m = sc.nextInt

    val rh = new RollingHash(s)

    val maps: Array[MHashMap[Long, Int]] = Array.fill(10)(new MHashMap)

    for(l <- 1 to 10){
      for(i <- 0 to (s.length - l)){
        val j = i + l
        val a = rh.hash(i,j)
        maps(l-1).get(a) match{
          case Some(x) => maps(l-1).update(a,x+1)
          case None => maps(l-1).update(a,1)
        }
      }
    }

    pw.println(
      (1 to m).map(_ => {
        val c = sc.next
        maps(c.length-1).get(rh.hash(c)) match{
          case Some(x) => x
          case None => 0
        }
      }).sum)

    pw.flush()
  }
}

class RollingHash(s: String){
  private val base: Long = 29
  private val m: Long = 1000000007
  private val sl = s.length
  private val hash: Array[Long] = s.scanLeft(0: Long)((a,b) => (a*base+b)%m).toArray
  private val pow: Array[Long] = (1 to sl).scanLeft(1: Long)((a,b) => a*base%m).toArray

  def hash(i: Int, j: Int): Long = (hash(j) - hash(i) * pow(j-i) + m*m) % m
  def hash(p: String): Long = p.foldLeft(0: Long)((a,b) => (a*base+b)%m)

  def find(p: String): Array[Int] = {
    val pl = p.length
    val phash: Long = p.foldLeft(0: Long)((a,b) => (a*base+b)%m)

    (0 to (sl-pl)).filter(i => {
      val j = i + pl
      hash(i,j) == phash
    }).toArray
  }
}
0