Solution Explanation

Султан Казино Отзывы For every position of the given string

'.' - the symbol is fixed
'?' - we may write '.' or 'X'

The task is to count how many complete strings can be obtained if we replace
every ‘?’ by one of the two symbols and no two neighbouring positions contain
‘X’.

Only the places that contain ‘?’ are interesting – the already written ‘.’ do
not influence the restriction.
Therefore the string can be split into blocks of consecutive ‘?’ symbols,
separated by ‘.’.
Blocks are completely independent, because a ‘.’ is never equal to ‘X’
and does not create a forbidden pair with a neighbour inside a block.

1. How many ways to fill one block ?

Consider a block of length L consisting only of ‘?’.
We have to write a binary word of length L (‘.’ = 0, ‘X’ = 1)
with no two consecutive 1’s.

Let f(L) be the number of such words.
The usual recurrence for binary strings without consecutive 1’s gives

f(0) = 1     // empty word
f(1) = 2     // 0 , 1
f(L) = f(L-1) + f(L-2)  (L ≥ 2)

casinoaviator.kz Thus f(L) is the Fibonacci number F[L+2]
(F[0] = 0, F[1] = 1).
The values grow fast, therefore all computations are performed modulo

MOD = 1 000 000 007

2. Whole string

Let the lengths of all blocks be L1 , L2 , … , Lk.
Because blocks are independent, the total number of admissible strings is

answer = Π f(Li)  (product over all blocks)

If the string contains no ‘?’ the product is empty and the answer equals 1.

3. Algorithm

read string s
ans = 1
cur = 0           // length of current block
for each character c in s
    if c == '?'
        cur += 1
    else        // c == '.'
        ans = ans * fib[cur+2] mod MOD
        cur = 0
// last block (if the string ends with '?')
ans = ans * fib[cur+2] mod MOD
output ans

fib[i] is pre‑computed once up to the maximum possible block length
(n ≤ 2·10^5, so fib up to n+2 is enough).

4. Correctness Proof

We prove that the algorithm outputs the required number of strings.

Lemma 1

For a block of length L the number of ways to replace its characters
by . and X without two consecutive X equals fib[L+2].

Proof.

The recurrence described in section 1 follows directly from the
definition of binary strings without consecutive 1’s:
a valid word of length L either starts with . followed by a valid word of
length L-1, or starts with X followed by a . and then a valid word of
length L-2.
The base cases L=0,1 are obvious.
Solving the recurrence gives the Fibonacci numbers F[L+2], which we store
in fib[L+2].∎

Lemma 2

Let the input string consist of blocks B1,…,Bk of consecutive ?
characters, separated by ..
The total number of admissible completions of the whole string equals
∏_i=1^k fib[|Bi|+2].

Proof.

Blocks are separated by fixed . symbols, which are never equal to X.
Consequently, the choice of symbols inside one block does not influence
any choice inside another block – the restrictions are local to each block.
By Lemma 1 each block Bi of length |Bi| can be filled in
fib[|Bi|+2] ways.
The product of the numbers of possibilities for all blocks gives the total
number of admissible completions.∎

Lemma 3

During its execution the algorithm multiplies ans exactly by
fib[|Bi|+2] for every block Bi of the input string.

Proof.

The algorithm scans the string from left to right.
While reading consecutive ? it increases cur – the current block length.
When a . is met, the block just finished has length cur; the algorithm
multiplies ans by fib[cur+2] and resets cur to zero.
After the loop finishes, the last block (if the string ends with ?)
is processed in the same way.
Hence each block contributes precisely one factor fib[|Bi|+2].∎

Theorem

The algorithm outputs the number of different strings obtainable by
replacing every ? with either . or X such that no two adjacent
positions contain X.

Proof.

By Lemma 3 the algorithm computes bitajon-israeli.com.ec the product
Π fib[|Bi|+2], where Bi are all blocks of ?.
By Lemma 2 this product equals the exact number of admissible
completions of the whole string.
Therefore the algorithm outputs the required number.∎

5. Complexity Analysis

Let n be the length of the input string (n ≤ 2·10^5).

Precomputation: O(n) time, O(n) memory for the Fibonacci array.
Main loop: each character is examined once → O(n) time, O(1) extra
memory.

Overall complexity: O(n) time, O(n) memory.

6. Reference Implementation (Python 3)

import sys

MOD = 10 9 + 7

def solve() -> None:
  s = sys.stdin.readline().strip()
  n = len(s)

  # precompute Fibonacci numbers up to n+2
  fib = [0] * (n + 3)
  fib[0] = 0
  fib[1] = 1
  for i in range(2, n + 3):
    fib[i] = (fib[i - 1] + fib[i - 2])% MOD

  ans = 1
  cur = 0 # current block length

  for ch in s:
    if ch == '?':
      cur += 1
    else:     # ch == '.'
      ans = (ans * fib[cur + 2])% MOD
      cur = 0

  # last block (if string ends with '?')
  ans = (ans * fib[cur + 2])% MOD

  print(ans)

if __name__ == "__main__":
  solve()

The program follows exactly the algorithm proven correct above
and conforms to the required time and memory limits.