Official

B - 1D Akari Editorial by en_translator


Let us review the conditions of the problem statement. They can be rephrased as follows:

  • Replace some occurrences of . with o.
  • But any pair of os must have # in between.
  • Maximize the number of o under these constraints.

Here, by the condition “any pair of os must have # in between,” it turns out that if we have multiple consecutive .s, we can replace at most one of them with o. For example, if \(S=\) ...#.., then we can replace at most one of ... to the left, and at most one of .. to the right, with o.

There are multiple ways to construct \(T\) subject to this condition. Here, let us impose an additional constraint: “we make o positioned as left as possible.” For example, if \(S=\) ...#.., we make \(T=\) o..#o., placing o to the leftmost position in each region.

Then, it turns out that it is sufficient to replace every #. with #o, with one exception at the beginning of \(T\). This exceptional position can also be covered by adding a virtual # as the \(0\)-th character of \(S\), allowing us to reducing caseworks.

Sample code (Python3)

print(("#"+input()).replace("#.","#o")[1:])

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
	string s;
	cin >> s;
	int n = s.size();
	vector<char> t(n);
	for (int i = 0; i < n; i++) {
		if (s[i] == '#') {
			cout << '#';
		} else if (i == 0 || s[i - 1] == '#') {
			cout << 'o';
		} else {
			cout << '.';
		}
	}
	cout << endl;
	return 0;
}

posted:
last update: