Official

D - Logical Filling Editorial by en_translator


This is a problem that requires careful case discussions.

First, replace all ? adjacent to o in \(S\) with ..

If \(S\) contain exactly \(K\) o’s

The only sought string is obtained by replacing all ? with ..

We consider the other cases. First, we find a way to fill ? to maximize the number of o as much as possible, without making two o’s adjacent. This can be done in a greedy fashion from left to right: for each position, we replace ? with o if it is not next to another o.

Let \(M\) be the number of o’s obtained by this procedure.

If \(M=K\)

We maximize the number of o. If there is an odd number of ?’s, the way to fill it is unique: we can fill it as o.o.o. If there is an even number of ?s, there are two patterns possible, like o.o. and .o.o.

Therefore, the answer can be obtained by replacing an odd number of consecutive ?’s with alternating o and ., and keeping an even number of consecutive ?’s.

If \(M>K\)

The answer is \(S\) itself.

Proof: by dropping some o’s from a string with the maximum o’s, most positions can be filled in two ways, except for an even number of consecutive ?’s. For such region, one can first fill it with .o.o. to achieve \((M-1)\) o’s; by dropping the others, any position can become o and ..

posted:
last update: