Problem
Получите бесплатные вращения в „nomad казино” уже сегодня: https://gitop.kz/go/41. You are given a string S of length N.
The string consists only of the following characters
.– a fixed dot, it cannot be changed;?– a free position, you may replace it by either.or by a lettera;a– a fixed lettera, it also cannot be changed.
You may replace every ? independently. After the replacements the string must not contain two consecutive a’s.
For the given string S find the maximum possible number of letters a that can appear in the final string.
Input
One line – the string S (1 ≤ |S| ≤ 10^5).
S contains only the characters ., ?, a.

Output
4vlastinfo.kz предлагает лучшие условия для новых пользователей. Print a single integer – the maximum possible number of a’s.
Example
Input
a.?a?....
Output
4
Explanation
One optimal replacement is
a a a a ...
which contains four a’s and has no two consecutive a’s.
Note
The task is essentially a greedy/dynamic‑programming problem on a line of cells,
each cell being either forced to be . / a or free to become one of them.
The only restriction is that two a’s may not sit next to each other.
Solution Idea
We scan the string from left to right and keep two values:
dp0– the maximum number ofa’s in a prefix that ends with a dot (so the last character is nota);dp1– the maximum number ofa’s in a prefix that ends with ana.
Initially dp0 = 0, dp1 = -∞ (impossible).
When we see a new character:
-
If the character is forced
.
→ the current cell must be a dot, therefore
new_dp0 = max(dp0, dp1)(we can come from either state);
new_dp1 = -∞. -
If the character is forced
a
→ the current cell must bea, therefore it can only follow a dot:
new_dp1 = dp0 + 1;
new_dp0 = -∞. -
If the character is
?
→ we may put a dot or ana. - Putting a dot gives the same transition as for a forced dot.
- Putting an
ais allowed only if the previous character was a dot, so we usedp0 + 1.
Hence
new_dp0 = max(dp0, dp1)
new_dp1 = dp0 + 1.
After processing the whole string the answer is max(dp0, dp1).
Correctness Proof
We prove that the algorithm returns the maximum possible number of a’s.
Lemma 1
After processing the first i characters, dp0 equals the maximum number of a’s
obtainable in a valid completion of the prefix S[1..i] that ends with a dot,
and dp1 equals the maximum number of a’s obtainable in a valid completion
that ends with an a.
Proof.
By induction on i.
Base (i = 0).
No characters processed: the empty prefix ends with neither a dot nor an a.
The algorithm sets dp0 = 0 (no a’s) and dp1 = -∞ (impossible).
Both statements hold.
Induction step.
Assume the lemma true for prefix length i-1.
Consider the i‑th character.
Case 1 – the character is ..
Any valid completion of the whole prefix must end with a dot.
The best we can do is to take the better of the two completions of the previous
prefix (dp0 or dp1) and keep the same number of a’s.
The algorithm sets new_dp0 = max(dp0,dp1) and new_dp1 = -∞,
exactly reflecting this.
Case 2 – the character is a.
Any valid completion must end with an a.
To stay valid the preceding character must be a dot, so the best
previous prefix is the one ending with a dot (dp0).
We add one a.
Thus new_dp1 = dp0 + 1 and new_dp0 = -∞.
The algorithm does the same.
Case 3 – the character is ?.
We may choose a dot or an a.
Choosing a dot behaves as in case 1, giving new_dp0 = max(dp0,dp1).
Choosing an a behaves as in case 2, giving new_dp1 = dp0 + 1.
The algorithm keeps both possibilities, therefore the lemma holds.
Thus the lemma is true for all i.∎
Lemma 2
At the end of the scan the value max(dp0,dp1) equals the maximum number of
a’s that can appear in any valid completion of the whole string.
Proof.
By Lemma 1 after processing all N characters, dp0 is the best
completion ending with a dot and dp1 is the best completion ending with an
a.
Any valid completion must end with either a dot or an a, so its number of
a’s is bounded above by max(dp0,dp1).
Conversely, the completion achieving dp0 or dp1 is valid, thus
max(dp0,dp1) is attainable.∎
Theorem
The algorithm outputs the maximum possible number of letters a that can
appear in the final string after replacing every ? with either . or a
under the constraint that no two a’s are adjacent.
Proof.
By Lemma 2 the algorithm’s output equals the optimum number of a’s.
Therefore the algorithm is correct.∎
Complexity Analysis
The algorithm scans the string once and performs only constant‑time work per
character.
- Time complexity:
O(N) - Memory usage:
O(1)
Reference Implementation (Python 3)
import sys
def solve() -> None:
s = sys.stdin.readline().strip()
INF_NEG = -109 # sufficiently small
dp0 = 0 # best up to now, ending with '.'
dp1 = INF_NEG # best up to now, ending with 'a'
for ch in s:
http://akhmadiinkhotkhon-1.ub.gov.mn/?p=9349 if ch == '.':
new_dp0 = max(dp0, dp1)
new_dp1 = INF_NEG
elif ch == 'a':
new_dp1 = dp0 + 1
new_dp0 = INF_NEG
else: # ch == '?'
new_dp0 = max(dp0, dp1) # choose dot
new_dp1 = dp0 + 1 # choose a
dp0, dp1 = new_dp0, new_dp1
print(max(dp0, dp1))
if __name__ == "__main__":
solve()
The program follows exactly the algorithm proven correct above and
conforms to the required input/output format.