結果
問題 | No.430 文字列検索 |
ユーザー | Haar |
提出日時 | 2018-10-11 20:44:18 |
言語 | Scala(Beta) (3.3.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,565 bytes |
コンパイル時間 | 9,724 ms |
コンパイル使用メモリ | 257,792 KB |
実行使用メモリ | 94,408 KB |
最終ジャッジ日時 | 2023-09-13 20:23:43 |
合計ジャッジ時間 | 35,502 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 999 ms
63,716 KB |
testcase_01 | WA | - |
testcase_02 | AC | 1,362 ms
67,148 KB |
testcase_03 | AC | 1,359 ms
67,136 KB |
testcase_04 | AC | 985 ms
63,892 KB |
testcase_05 | AC | 989 ms
63,964 KB |
testcase_06 | AC | 1,009 ms
63,752 KB |
testcase_07 | AC | 1,013 ms
63,864 KB |
testcase_08 | WA | - |
testcase_09 | AC | 1,025 ms
63,756 KB |
testcase_10 | WA | - |
testcase_11 | AC | 1,446 ms
69,580 KB |
testcase_12 | AC | 1,475 ms
70,152 KB |
testcase_13 | AC | 1,465 ms
69,476 KB |
testcase_14 | AC | 1,453 ms
69,616 KB |
testcase_15 | AC | 1,441 ms
67,404 KB |
testcase_16 | AC | 1,427 ms
67,096 KB |
testcase_17 | AC | 1,476 ms
67,260 KB |
ソースコード
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 = 10 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 } }