結果

問題 No.1859 ><<<
ユーザー yudedako
提出日時 2022-03-14 17:09:36
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,537 ms / 2,000 ms
コード長 1,663 bytes
コンパイル時間 11,783 ms
コンパイル使用メモリ 273,328 KB
実行使用メモリ 100,348 KB
最終ジャッジ日時 2024-09-20 04:23:20
合計ジャッジ時間 79,038 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import scala.collection.mutable.*
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.*
import scala.annotation.tailrec
import scala.collection.mutable


def powMod(base: Long, exp: Int, mod: Int): Long =
  var result = 1L
  var b = base % mod
  var e = exp
  while e > 0 do
    if (e & 1) == 1 then
      result = result * b % mod
    b = b * b % mod
    e >>= 1
  result

@main def main() =
  inline val MOD = 1_000_000_007
  val random = Random(0)
  val n = readLine().toInt
  val num = readLine().split(' ').map(_.toInt)
  val str = readLine()
  val isMatch = Array.fill(2 * n - 1){true}
  val double = (num ++ num).pipe{array =>
    (1 until array.length).map{i => if array(i - 1) < array(i) then 0 else 1}.toArray
  }
  val target = str.map{
    case '<' => 0
    case '>' => 1
  }.toArray
  val left = powMod(2, n - 1, MOD)
  for _ <- 0 until 3 do
    val x = random.nextInt(MOD)
    val targetHash = target.foldLeft(0L){ (acc, v) => ((acc << 1) + v * x) % MOD}
    var hash = target.indices.foldLeft(0L){ (acc, i) => ((acc << 1) + double(i) * x) % MOD}
    isMatch(target.length - 1) = hash == targetHash
    //println(s"$hash, $targetHash")
    for i <- target.length until double.length do
      hash = ((hash << 1) + double(i) * x) % MOD
      hash = (hash - left * double(i - target.length) * x % MOD + MOD) % MOD
      isMatch(i) = isMatch(i) && hash == targetHash
      //println(s"$hash, $targetHash")
  (target.length - 1 until isMatch.length).find(isMatch(_)) match
    case Some(i) => println(i - target.length + 1)
    case None => println(-1)



0