C - String Invasion Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

長さ NN の文字列 SS が与えられます。SSii 文字目を sis_i で表します。以下の操作を繰り返せる回数の最大値を求めてください。

  • 連続する 33 文字 si,si+1,si+2(1iS2)s_i,s_{i+1},s_{i+2}\quad (1\leq i\leq |S|-2) であって、si=si+1si+2s_i=s_{i+1}\neq s_{i+2} であるものを選ぶ。si+2s_{i+2}sis_i で置き換える。

制約

  • 3S2×1053 \leq |S| \leq 2\times 10^5
  • SS は英小文字からなる

入力

入力は以下の形式で標準入力から与えられる。

SS

出力

操作を繰り返せる回数の最大値を出力せよ。


入力例 1Copy

Copy
accept

出力例 1Copy

Copy
3

以下のように 33 回の操作を行うことができます。

  • i=2i=2 に対して操作を行う。操作後の文字列は acccpt になる。
  • i=3i=3 に対して操作を行う。操作後の文字列は acccct になる。
  • i=4i=4 に対して操作を行う。操作後の文字列は accccc になる。

入力例 2Copy

Copy
atcoder

出力例 2Copy

Copy
0

入力例 3Copy

Copy
anerroroccurred

出力例 3Copy

Copy
16

Score : 500500 points

Problem Statement

Given is a string SS of length NN. Let sis_i denote the ii-th character of SS. Find the maximum number of times the following operation can be done.

  • Choose three consecutive characters in SS, si,si+1,si+2(1iS2)s_i,s_{i+1},s_{i+2}\quad (1\leq i\leq |S|-2), such that si=si+1si+2s_i=s_{i+1}\neq s_{i+2}, and replace si+2s_{i+2} with sis_i.

Constraints

  • 3S2×1053 \leq |S| \leq 2\times 10^5
  • SS consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

SS

Output

Print the maximum number of times the operation can be done.


Sample Input 1Copy

Copy
accept

Sample Output 1Copy

Copy
3

We can do the operation three times, as follows:

  • do it with i=2i=2, changing the string to acccpt;
  • do it with i=3i=3, changing the string to acccct;
  • do it with i=4i=4, changing the string to accccc.

Sample Input 2Copy

Copy
atcoder

Sample Output 2Copy

Copy
0

Sample Input 3Copy

Copy
anerroroccurred

Sample Output 3Copy

Copy
16


2025-04-24 (Thu)
23:12:38 +00:00