結果

問題 No.238 Mr. K's Another Gift
ユーザー ともきともき
提出日時 2015-07-06 04:34:14
言語 Scala(Beta)
(3.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,333 bytes
コンパイル時間 10,741 ms
コンパイル使用メモリ 259,624 KB
実行使用メモリ 83,628 KB
最終ジャッジ日時 2023-09-11 11:41:56
合計ジャッジ時間 78,170 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 996 ms
63,636 KB
testcase_01 AC 997 ms
63,564 KB
testcase_02 AC 1,004 ms
63,416 KB
testcase_03 AC 1,035 ms
63,820 KB
testcase_04 AC 1,124 ms
63,720 KB
testcase_05 AC 1,760 ms
73,560 KB
testcase_06 AC 1,770 ms
80,896 KB
testcase_07 AC 1,939 ms
83,192 KB
testcase_08 AC 1,955 ms
81,560 KB
testcase_09 AC 1,793 ms
81,096 KB
testcase_10 AC 978 ms
63,520 KB
testcase_11 AC 988 ms
63,472 KB
testcase_12 AC 1,032 ms
63,864 KB
testcase_13 AC 1,104 ms
63,888 KB
testcase_14 AC 1,567 ms
68,348 KB
testcase_15 AC 1,994 ms
83,324 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 1,996 ms
83,212 KB
testcase_19 AC 1,998 ms
83,372 KB
testcase_20 AC 996 ms
63,696 KB
testcase_21 AC 977 ms
63,512 KB
testcase_22 AC 973 ms
63,416 KB
testcase_23 AC 1,019 ms
63,576 KB
testcase_24 AC 1,128 ms
64,112 KB
testcase_25 AC 1,117 ms
63,844 KB
testcase_26 AC 1,685 ms
79,824 KB
testcase_27 AC 1,785 ms
79,712 KB
testcase_28 AC 1,151 ms
72,156 KB
testcase_29 AC 1,776 ms
79,740 KB
testcase_30 AC 996 ms
63,696 KB
testcase_31 AC 1,020 ms
63,496 KB
testcase_32 AC 1,052 ms
63,720 KB
testcase_33 AC 1,191 ms
63,648 KB
testcase_34 AC 1,877 ms
74,240 KB
testcase_35 AC 1,949 ms
80,760 KB
testcase_36 AC 1,940 ms
82,472 KB
testcase_37 TLE -
testcase_38 AC 1,945 ms
81,892 KB
testcase_39 AC 1,949 ms
81,808 KB
testcase_40 AC 994 ms
63,928 KB
testcase_41 AC 988 ms
63,424 KB
testcase_42 AC 994 ms
63,500 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 insert(s : String, c : Char, n : Int) : String = {
  //   val (head,tail) = s.splitAt(n)
  //   head + c + tail
  // }
  // def solve(s : String) : Option[String] = {
  //   val n = s.length
  //   val b = new Breaks
  //   def check(s : String,c : Char) : Boolean = {
  //     val n = s.length
  //     (0 until s.length).forall((i) => {
  //                                 val j = n - i - 1
  //                                 s(i) == c || s(j) == c || s(i) == s(j)
  //                               })
  //   }
  //   if(check(s,'?')){
  //     Some(s.take(n/2) + s(n/2) + s.drop(n/2))
  //   }else{
  //     var ans : Option[String] = None
  //     b.breakable {
  //       for(i <- 0 until n){
  //         val j = n - i - 1
  //         if(s(i) != s(j)){
  //           val checking = Vector((s.take(i) + "?" + s.drop(i)),
  //                                 (s.take(j+1) + "?" + s.drop(j+1)))
  //           checking.foreach((i) => if(check(i,'?')){
  //                              val r = i.indexOf('?')
  //                              ans = Some(i.updated(r,i(n-r)))
  //                            })
  //           b.break
  //         }
  //       }
  //     }
  //     ans
  //   }
  // }
  // rsexr
  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))
     //  .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