Editorial for JOI '05 Final Round P2 - String


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

与えられた文字列に問題の操作を 1 回施す方法だけを解説しよう.これを n 回繰り返せばよい.

まず,文字列の左端の文字に注目し,2 文字目以降を検査して行きながら,現在注目している文字と同じときはカウントを増加させ,異なるときは現在のカウントと注目している文字を新しい文字列に書き加え,注目している文字をそのとき読んだ文字に変更し,カウントを 1 に戻す.この作業を文字列の右端まで行う.あらかじめ文字列の右端に数字以外の文字,例えば空白文字を 番兵 (centinel) として加えておけば,文字列を左端から右端まで 1 度読むだけで操作は終了する.

最後に,プログラミング言語に依存することであるが,式

result = result + toString (count) + toString (ch)

を実現するには, C 言語では

  sprintf(result, "%s%d%c", result, count, ch)

Java では

  result = result + count + ch

とすればよい.


Comments

There are no comments at the moment.