結果

問題 No.996 Phnom Penh
ユーザー yakamotoyakamoto
提出日時 2020-02-24 22:01:22
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 4,544 bytes
コンパイル時間 17,938 ms
コンパイル使用メモリ 455,612 KB
実行使用メモリ 92,064 KB
最終ジャッジ日時 2024-10-12 14:11:10
合計ジャッジ時間 30,398 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
50,156 KB
testcase_01 AC 277 ms
49,968 KB
testcase_02 AC 281 ms
49,960 KB
testcase_03 AC 288 ms
49,940 KB
testcase_04 AC 282 ms
49,864 KB
testcase_05 AC 292 ms
50,004 KB
testcase_06 AC 282 ms
49,948 KB
testcase_07 AC 290 ms
50,124 KB
testcase_08 AC 287 ms
49,996 KB
testcase_09 AC 289 ms
50,068 KB
testcase_10 AC 285 ms
49,920 KB
testcase_11 AC 284 ms
50,140 KB
testcase_12 AC 298 ms
49,876 KB
testcase_13 AC 529 ms
68,168 KB
testcase_14 AC 539 ms
67,552 KB
testcase_15 AC 536 ms
67,696 KB
testcase_16 AC 533 ms
68,764 KB
testcase_17 AC 527 ms
67,616 KB
testcase_18 AC 283 ms
49,848 KB
testcase_19 AC 280 ms
49,760 KB
testcase_20 AC 277 ms
49,832 KB
testcase_21 AC 293 ms
49,860 KB
testcase_22 AC 287 ms
49,840 KB
testcase_23 AC 284 ms
49,820 KB
testcase_24 AC 696 ms
92,064 KB
testcase_25 WA -
testcase_26 AC 598 ms
78,168 KB
testcase_27 AC 546 ms
76,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val base = ns()
    val N = base.length
    val S = CharArray(N + 2)
    val next = IntArray(N + 2)
    val prev = IntArray(N + 2)
    val ps = mutableSetOf<Int>()
    val es = mutableSetOf<Int>()
    val hs = mutableSetOf<Int>()
    next[0] = 1
    prev[N + 1] = N
    for (i in 1..N) {
      next[i] = i + 1
      prev[i] = i - 1
      S[i] = base[i - 1]

      when {
        S[i] == 'p' -> ps += i
        S[i] == 'h' -> hs += i
        S[i] == 'e' -> es += i
      }
    }

    fun testWord(s: Int, word: String, i: Int): Boolean {
      if (i == word.length) return true
      if (S[s] != word[i]) return false
      return testWord(next[s], word, i + 1)
    }

    fun startsWithPhnom(i: Int) = testWord(i, "phnom",0)

    fun replaceWithWord(s: Int, word: String, i: Int): Int {
      if (i == word.length) return s
      S[s] = word[i]
      return replaceWithWord(next[s], word, i + 1)
    }

    fun removeChar(i: Int) {
      prev[next[i]] = prev[i]
      next[prev[i]] = next[i]
    }

    fun replaceWithPenh(i: Int) {
      val ix_m = replaceWithWord(i, "penh", 0)
      removeChar(ix_m)

      S[next[i]] = 'e'
      hs -= next[i]
      es += next[i]
      hs += prev[ix_m]
    }

    fun findPrev(i: Int, c: Char, cnt: Int): Int {
      if (prev[i] == 0 || cnt == 0) return -1
      if (S[prev[i]] == c) return prev[i]
      return findPrev(prev[i], c, cnt - 1)
    }

    fun printS() {
      if (isDebug) {
        val str = StringBuffer()
        var i = next[0]
        while (i < N + 1) {
          str.append(S[i])
          i = next[i]
        }
        debug { str.toString() }
      }
    }

    var ans = 0
    loop@while(ps.isNotEmpty() || es.isNotEmpty() || hs.isNotEmpty()) {
      debug{"$ps $hs $es $ans"}
      printS()

      val itr_p = ps.iterator()
      while(itr_p.hasNext()) {
        val i = itr_p.next()
        itr_p.remove()

        if (startsWithPhnom(i)) {
          ++ans
          replaceWithPenh(i)
          continue@loop
        }
      }

      if (es.isNotEmpty() || hs.isNotEmpty()) {
        ++ans
        val itr_h = hs.iterator()
        while(itr_h.hasNext()) {
          val i = itr_h.next()
          itr_h.remove()
          removeChar(i)
          val ixP = findPrev(i, 'p', 4)
          if (ixP != -1) ps += ixP
        }
        val itr_e = es.iterator()
        while(itr_e.hasNext()) {
          val i = itr_e.next()
          itr_e.remove()
          S[i] = 'h'
          hs += i
          if (prev[i] != -1 && S[i] == 'p') ps += prev[i]
        }
      }
    }
    out.println(ans)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n, offset) { ni() }
  }

  private inline fun map(n: Int, offset: Int = 0, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i + offset)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun <A, B> pair(a: A, b: B) = RPair(a, b)
data class RPair<A, B>(val _1: A, val _2: B)

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0