Official

B - Longest Rainy Streak Editorial by en_translator


Use a loop structure like for statement to inspect the characters in the string from the beginning.

  • Let \(cur\) be the length of the run of R up to the current point, and \(res\) be the answer. Initialize them with \(cur=0\) and \(res=0\).
  • For \(i=1,2,\dots,N\), do the following:
    • If the \(i\)-th character of \(S\) is R, add \(1\) to \(cur\).
    • Otherwise, the run of R no longer continues, so set \(cur=0\).
    • Set \(res\) to \(\max(res,cur)\).

This way, the number of the longest run can be memorized for sure once it was found.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cur=0,res=0;
  for(auto &nx : s){
    if(nx=='R'){cur++;}
    else{cur=0;}
    res=max(res,cur);
  }
  cout << res << "\n";
  return 0;
}

Original proposer: admin