結果

問題 No.238 Mr. K's Another Gift
ユーザー ともきともき
提出日時 2015-07-06 04:28:43
言語 Scala(Beta)
(3.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,043 bytes
コンパイル時間 8,193 ms
コンパイル使用メモリ 271,448 KB
最終ジャッジ日時 2024-04-27 02:08:30
合計ジャッジ時間 8,964 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
-- Error: Main.scala:76:5 ------------------------------------------------------
76 |     (for(i <- 0 to n;
   |     ^
   |     end of statement expected but '(' found
-- [E007] Type Mismatch Error: Main.scala:77:37 --------------------------------
77 |         c <- 'a' to 'z') yield (i,c))
   |                                     ^
   |                                     Found:    Unit
   |                                     Required: Option[String]
   |
   | longer explanation available when compiling with `-explain`
2 errors found

ソースコード

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
    }
     (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