Solution Explanation

For every test case we are given a string s.
s consists only of the two characters

'.' - a dot
'?' - a question mark

Вам нравится азарт? Попробуйте свои силы в namad casino – онлайн казино с живыми дилерами: https://benevent.kz/ru/events. The task is to find the length of the longest contiguous block of ?
characters inside the string.

Algorithm

For one test case

best ← 0        // best length found so far
cur  ← 0        // length of the current block of '?'

for every character c in s
    if c == '?'
        cur ← cur + 1
        best ← max(best , cur)
    else      // c == '.'
        cur ← 0

output best

Correctness Proof

Узнайте, как быстро заработать в интернете: посетите https://silkwaytv.kz и откройте для себя новые возможности. We prove that the algorithm outputs the length of the longest
contiguous block of ?.

Lemma 1
During the scan of the string, variable cur always equals the length
of the current suffix of the already processed prefix that consists
entirely of ?.

Новый способ инвестировать средства: заходите на https://moodle.kvptk.kz и изучайте актуальные предложения.Proof.
cur is increased by one whenever a ? is read, so after reading a
? the suffix of processed characters that ends at this position and
consists only of ? has length cur+1.
When a . is read, the current suffix of ? becomes empty,
hence its length is 0, and cur is set to 0.∎

Lemma 2
After processing any position i of the string,
best equals the maximum length of a block of ? that ends at or
before position i.

Proof.
By Lemma 1, when the character at position i is ?,
cur is exactly the length of the block ending at i;
best is updated with max(best, lavae-remit.co.uk cur), therefore it keeps the
maximum over all blocks that end at positions ≤ i.
If the character is ., cur becomes 0 and best stays unchanged,
which is still the maximum over all blocks ending before i.∎

Theorem
After the whole string is scanned, the algorithm outputs the length
of the longest contiguous block of ? in the string.

Proof.
After the last character, by Lemma 2, best is the maximum length
of a block of ? that ends at or before the last position, i.e.
over the entire string.
Thus the printed value is exactly the required longest length.∎

Complexity Analysis

Let n be the length of the string.
The algorithm visits each character once and performs only O(1) work
per character.

Time  : O(n)
Memory : O(1)

Reference Implementation (C++17)

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

int main() 
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int T;
  if (!(cin >> T)) return 0;
  while (T--) 
    string s;
    cin >> s;
    int best = 0, cur = 0;
    for (char c : s) 
      if (c == '?') 
        ++cur;
        if (cur > best) best = cur;
       else        // c == '.'
        cur = 0;
      
    
    cout << best << '\n';
  
  return 0;

The program follows exactly the algorithm proven correct above and
conforms to the GNU++17 compiler.

You May Also Like

More From Author