Official

B - Longest Rainy Streak Editorial by physics0523


for ループなどで文字列を先頭から順に調べます。

  • 現在続いている R の個数 \(cur=0\) 、答え \(res=0\) と初期化する。
  • \(i=1,2,\dots,N\) について、以下を繰り返す。
    • もし \(S\) の \(i\) 文字目が R なら \(cur\) に \(1\) 加算する。
    • そうでなければ R の連続が途切れるため、 \(cur=0\) とする。
    • \(res\) を \(\max(res,cur)\) に更新する。

こうすることで、最も多くの R が連続しているタイミングでその数を確実に記録しておけます。

実装例 (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;
}

原案: admin