DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Deletion 2

Back to problem

Editorial

A terminal string has no two adjacent different characters. Therefore it is either empty or consists of only one kind of character.

Lemma 1. A string WWW of length mmm can be completely deleted if and only if mmm is even and every character appears at most m/2m/2m/2 times.

Proof

Whenever one character is deleted, a different character must be deleted with it, so necessity is immediate.

For sufficiency, use induction on mmm. If some character appears exactly m/2m/2m/2 times, delete a pair at a boundary between that character and another character. Otherwise, delete any adjacent pair of different characters. In both cases the remaining string satisfies the same condition.

Fix a character XXX and consider terminal strings consisting only of XXX. Let the other two characters be Y,ZY,ZY,Z.

After choosing the occurrences of XXX that remain, every segment between them and at both ends must disappear.

Lemma 2. It is enough to require that, in each such segment, both YYY and appear at most half of the segment length. This does not change the maximum possible number of surviving 's.

Proof

Every completely deletable segment satisfies this condition by Lemma 1.

Conversely, if a segment satisfying this condition also contains at most half XXX, the whole segment can be deleted by Lemma 1. If XXX appears more than half the time, every Y,ZY,ZY,Z can be deleted together with an XXX, leaving only XXX's in that segment.

Let this maximum be MXM_XMX​.

Let Yi,ZiY_i,Z_iYi​,Zi​ be the numbers of in . For a position with , define and .

Proof

Let XiX_iXi​ be the number of XXX in S[1…i]S[1\ldots i]S[1…i]. Then ui+v.

By Lemma 3, the index condition is unnecessary. Process the points by uuu and maintain the dominance maximum by vvv with a Fenwick Tree. Keep the two parities separately. This finds MXM_XMX​ in O(Nlog⁡N)O(N\log N)O(Nlo.

Proof

Suppose XkX^kXk is reachable and k>LXk>L_Xk>LX​. Let G0,G1,…, be the completely deleted segments around the surviving 's.

Therefore, if MX>0M_X>0MX​>0, the number of terminal strings consisting only of XXX is (MX−LX)/2+1(M_X-L_X)/2+1(M.

ZZ
Z
XXX

Hence, if kkk occurrences of XXX can remain under this condition, actual operations can leave at least kkk copies of XXX. Therefore the maximum is unchanged.

Y,ZY,ZY,Z
S[1…i]S[1\ldots i]S[1…i]
iii
Si=XS_i=XSi​=X
ui=i−2Ziu_i=i-2Z_iui​=i−2Zi​
vi=i−2Yiv_i=i-2Y_ivi​=i−2Yi​

Two occurrences j<ij<ij<i of XXX can be consecutive survivors exactly when the segment between them satisfies Lemma 2. This is equivalent to uj<uiu_j<u_iuj​<ui​, vj<viv_j<v_ivj​<vi​, and j≢i(mod2)j\not\equiv i\pmod 2j≡i(mod2).

A first surviving XXX at position iii must satisfy that iii is odd, ui>0u_i>0ui​>0, and vi>0v_i>0vi​>0. Let U=N−2ZNU=N-2Z_NU=N−2ZN​ and V=N−2YNV=N-2Y_NV=N−2YN​. A last surviving XXX at position iii must satisfy i≡N(mod2)i\equiv N\pmod 2i≡N(mod2), ui≤Uu_i\le Uui​≤U, and vi≤Vv_i\le Vvi​≤V.

Let dpidp_idpi​ be the maximum number of surviving XXX's when position iii is the last survivor. A predecessor jjj must satisfy j<ij<ij<i, uj<uiu_j<u_iuj​<ui​, vj<viv_j<v_ivj​<vi​, and have the opposite parity. This is a three-dimensional dominance DP on the index, uuu, and vvv, so CDQ divide and conquer finds MXM_XMX​ in O(Nlog⁡2N)O(N\log^2 N)O(Nlog2N).

Lemma 3. Among positions with Si=XS_i=XSi​=X, if uj<uiu_j<u_iuj​<ui​ and vj<viv_j<v_ivj​<vi​, then j<ij<ij<i always holds.

i=2Xiu_i+v_i=2X_i
ui​+vi​=2Xi​

Among positions containing XXX, XiX_iXi​ is strictly increasing in their original order. Thus uj<uiu_j<u_iuj​<ui​ and vj<viv_j<v_ivj​<vi​ imply uj+vj<ui+viu_j+v_j<u_i+v_iuj​+vj​<ui​, so Xj<XiX_j<X_iXj​<Xi​ and therefore j<ij<ij<i.

g
N
)

Repeat this for X=A,B,CX=\texttt{A},\texttt{B},\texttt{C}X=A,B,C.

Suppose XXX appears xxx times in the whole string. If the terminal string is XkX^kXk, then k≡N(mod2)k\equiv N\pmod 2k≡N(mod2) because every operation removes two characters. Also, deleting one XXX requires one non-XXX, so k≥2x−Nk\ge 2x-Nk≥2x−N.

Thus the smallest positive possible length is LX=max⁡(2x−N,1)L_X=\max(2x-N,1)LX​=max(2x−N,1) when NNN is odd, and LX=max⁡(2x−N,2)L_X=\max(2x-N,2)LX​=max(2x−N,2) when NNN is even.

Lemma 4. If MX>0M_X>0MX​>0, the possible lengths are exactly LX,LX+2,…,MXL_X,L_X+2,\ldots,M_XLX​,LX​+2,…,MX​.

GkG_0,G_1,\ldots,G_k
G0​,G1​,…,Gk​
kkk
XXX

Let mrm_rmr​ be the length of GrG_rGr​ and xrx_rxr​ the number of XXX in it. Then dr=mr/2−xrd_r=m_r/2-x_rdr​=mr​/2−xr​ is a nonnegative integer. Also, ∑dr=(N−2x+k)/2>0\sum d_r=(N-2x+k)/2>0∑dr​=(N−2x+k)/2>0, so some drd_rdr​ is positive.

Delete two consecutive surviving XXX's so that the merged segment contains such a segment. In the merged segment, all three characters appear at most half of its length. By Lemma 1, the whole merged segment can be deleted, so Xk−2X^{k-2}Xk−2 is also reachable.

Repeating this gives every length LX,LX+2,…,MXL_X,L_X+2,\ldots,M_XLX​,LX​+2,…,MX​.

X
​
−
LX​)/2+
1

By Lemma 1, the empty string is reachable exactly when NNN is even and 2max⁡(#A,#B,#C)≤N2\max(\#A,\#B,\#C)\le N2max(#A,#B,#C)≤N. Add the counts for the three characters, and add one more if the empty string is reachable.

Therefore, the overall time complexity is O(Nlog⁡N)O(N\log N)O(NlogN).

+
vi​