結果

問題 No.238 Mr. K's Another Gift
ユーザー ともきともき
提出日時 2015-07-06 04:46:07
言語 Scala(Beta)
(3.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,257 bytes
コンパイル時間 9,417 ms
コンパイル使用メモリ 261,444 KB
実行使用メモリ 175,568 KB
最終ジャッジ日時 2023-09-11 11:44:31
合計ジャッジ時間 84,588 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 985 ms
63,392 KB
testcase_01 AC 980 ms
63,428 KB
testcase_02 AC 1,013 ms
63,544 KB
testcase_03 AC 1,108 ms
63,864 KB
testcase_04 AC 1,345 ms
71,152 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 AC 999 ms
63,416 KB
testcase_11 AC 1,006 ms
63,480 KB
testcase_12 AC 1,037 ms
63,720 KB
testcase_13 AC 1,139 ms
63,928 KB
testcase_14 AC 1,927 ms
133,076 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 990 ms
63,508 KB
testcase_21 AC 989 ms
63,340 KB
testcase_22 AC 1,003 ms
63,636 KB
testcase_23 AC 1,075 ms
64,100 KB
testcase_24 AC 1,301 ms
68,116 KB
testcase_25 AC 1,332 ms
81,440 KB
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 1,734 ms
172,676 KB
testcase_29 TLE -
testcase_30 AC 994 ms
63,600 KB
testcase_31 AC 993 ms
63,408 KB
testcase_32 AC 1,087 ms
64,072 KB
testcase_33 AC 1,355 ms
71,192 KB
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 AC 991 ms
63,424 KB
testcase_41 AC 986 ms
63,372 KB
testcase_42 AC 995 ms
63,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.io.StdIn.readLine
import scala.collection.mutable.PriorityQueue
import scala.annotation.tailrec
import scala.util.control.Breaks

object Lib {
  object String {
    // ex. b = 26 (if alphabet), mod = 1000000007
    class RollingHash[T](val b : Long,val mod : Long,val conv : T => Long) {
      final def hash(n : T) : Long = conv(n)%mod
      // n = (c0,c1,c2,c3,...cn)
      //  => (c0*b^n + ... + cn*b^0) mod 'mod'
      final def hash(n : Traversable[T]) =
        n.foldLeft(0l)((c,t) => (conv(t)+b*c)%mod)
      final def scan(n : Traversable[T]) =
        n.scanLeft(0l)((c,t) => (conv(t)+b*c)%mod)
      final def apply(n : T) = hash(n)
      final def apply(n : Traversable[T]) = hash(n)
    }
    object RollingHash {
      def apply[T](b : Long, mod : Long, conv : T => Long) = new RollingHash(b,mod,conv)
    }
  }
}

object Main {
  def solve(s : String) : Option[String] = {
    val r = Lib.String.RollingHash(103,1000000007,(c : Char) => c-'a'+1)
    val a = r.scan(s).toVector
    val b = r.scan(s.reverse).toVector
    val n = s.length
    val p = (0 to n).scanLeft(1l)((i,j) => (i*r.b % r.mod))
    def insert(hashlist : Vector[Long], c : Char, i : Int) : Long = {
      val left  = (hashlist(i) * p(n-i))       % r.mod
      val right = (hashlist(n) - left + r.mod) % r.mod
      val ins   = (r(c) * p(n-i))              % r.mod
      val here = (r.b * left + ins + right)    % r.mod
      here
    }

    // var ret : Option[String] = None
    // val bk = new Breaks
    // bk.breakable {
    //   for(i <- 0 to n;
    //       c <- 'a' to 'z'){
    //     if(insert(a,c,i) == insert(b,c,n-i)){
    //       ret = Some(s.take(i) + c + s.drop(i))
    //       bk.break
    //     }
    //   }
    // }
    // ret
     (for(i <- 0 to n;
          c <- 'a' to 'z') yield (i,c))
       .find({case (i,c) => insert(a,c,i) == insert(b,c,n-i)})
       .map({case (i,c) => s.take(i) + c + s.drop(i)})
      // .filter({case (i,c) => insert(a,c,i) == insert(b,c,n-i)})
     //  .headOption
     //  .map({case (i,c) => s.take(i) + c + s.drop(i)})
  }

  def main(args : Array[String]) : Unit = {
    val s = readLine
    val ret = solve(s) match {
        case Some(j) => j
        case None    => "NA"
      }
    println(ret)
  }
}
0