DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

Writing with a Stamp

Back to problem

Editorial

Using a stamp is free, so apply it at every matching occurrence. For a stamp of length xxx, let its occurrence endpoints be p1<⋯<pkp_1<\cdots<p_kp1​<⋯<pk​, with adjacent gaps di=pi+1−pid_i=p_{i+1}-p_idi​=pi+1​−pi​. Coverage and effort are

C(x)=x+∑i=1k−1min⁡(di,x),F(x)=Bx+A(N−C(x)).C(x)=x+\sum_{i=1}^{k-1}\min(d_i,x),\qquad F(x)=Bx+A(N-C(x)).C(x)=x+i=1∑

Also consider the no-stamp cost ANANAN.

Each suffix automaton state vvv represents substrings with the same occurrence endpoint set and lengths

L=len⁡(link⁡(v))+1throughR=len⁡(v).L=\operatorname{len}(\operatorname{link}(v))+1 \quad\text{through}\quad R=\operatorname{len}(v).L=len(link(v))+1throughR=len(v).

Find an optimal length in this interval for each state.

If B≤AB\leq AB≤A, choose x=Rx=Rx=R. Otherwise, set r=⌊(B−1)/A⌋r=\lfloor(B-1)/A\rfloorr=⌊(B−1)/A. If , choose . In the remaining case, find the -th largest gap and choose

x=min⁡(R,max⁡(L,d)).x=\min(R,\max(L,d)).x=min(R,max(L,d)).

If c,zc,zc,z are the count and sum of gaps smaller than the chosen length, evaluate coverage as C(x)=x+z+(k−1−c)xC(x)=x+z+(k-1-c)xC(x)=x+z+(k−1−.

Obtain endpoint sets by merging upward along the suffix-link tree. Always insert the smaller set into the larger one: each endpoint moves only O(log⁡N)O(\log N)O(logN) times. Maintain endpoints in an ordered set. Inserting an endpoint removes the old gap between its neighbors and creates up to two new gaps. A segment tree storing gap counts and sums supports these updates, prefix statistics, and order-statistic queries in O(log⁡N)O(\log N)O(logN) each.

The answer is the minimum over all state optima and ANANAN. The total complexity is O(Nlog⁡2N)O(N\log^2 N)O(Nlog2N) time and O(Nlog⁡N)O(N\log N)O(NlogN space.

Proof

All matching occurrences of a stamp agree wherever they overlap. The first occurrence covers xxx positions, and every subsequent occurrence adds min⁡(di,x)\min(d_i,x)min(di​,x) new positions, giving the formula for C(x)C(x)C(x). Every substring belongs to one SAM state and length, so optimizing every state considers every useful stamp.

k−1
​
min
(
di​
,
x
)
,
F
(
x
)
=
Bx+
A(N−
C(x)).
⌋
r>k−1r>k-1r>k−1
x=Lx=Lx=L
rrr
ddd
c)x
)

For a fixed endpoint set,

F(x)−F(x−1)=B−A(1+#{i:di≥x}).F(x)-F(x-1)=B-A\bigl(1+\#\{i:d_i\geq x\}\bigr).F(x)−F(x−1)=B−A(1+#{i:di​≥x}).

This difference is nondecreasing, making FFF discretely convex. If B≤AB\leq AB≤A, the difference is always nonpositive, so RRR is optimal. If B>AB>AB>A and r>k−1r>k-1r>k−1, it is always positive, so LLL is optimal. Otherwise, the difference is nonpositive through the rrr-th largest gap ddd and positive afterward. Clamping ddd to [L,R][L,R][L,R] therefore gives an optimum.