公式

A - 連続勝利の記録 / Record of Consecutive Wins 解説 by admin

gemini-3-flash-thinking

Overview

This problem asks you to find the length of the longest consecutive sequence of W (wins) in a match record consisting of W (wins) and L (losses) — in other words, the longest winning streak.

Analysis

This is a classic string processing problem: finding the maximum number of consecutive occurrences of a specific character within a string.

Key Insight

If we focus on the parts where W appears consecutively, we can see that they are always separated by L. For example, given the record S = "WWLWLWWW", splitting at L boundaries gives us the chunks "WW", "W", and "WWW", each consisting only of W.

By calculating the length of each chunk and finding the largest among them, we obtain the desired “longest consecutive win count.”

Approach

  1. Split the string by L.
  2. Measure the length of each resulting substring (consecutive W segments).
  3. Find the maximum of those lengths.

This method only requires a single pass through the string, so it runs efficiently even for the constraint \(N \leq 10^6\).

Algorithm

We make use of Python’s convenient string manipulation methods.

  1. Split: Use s.split('L') to create a list by splitting the string at every L.
    • Example: "WWLWLWWW".split('L') → ['WW', 'W', 'WWW']
  2. Convert to lengths: Use map(len, ...) to convert each element in the list to its length.
    • Example: ['WW', 'W', 'WWW'] → [2, 1, 3]
  3. Get the maximum: Use max(...) to extract the maximum value from the list of lengths.
    • Example: max([2, 1, 3]) → 3

Complexity

  • Time complexity: \(O(N)\)
    • Reading the string, splitting, computing lengths, and finding the maximum are all performed in linear time (proportional to the string length \(N\)).
  • Space complexity: \(O(N)\)
    • Storing the list of split substrings requires memory proportional to \(N\) in the worst case.

Implementation Notes

  • Handling large input: Since \(N\) can be as large as \(10^6\), we use sys.stdin.read().split() to read all input at once, reducing execution time.

  • Handling the case with no wins: When using the split('L') approach, even if the string contains no W at all (e.g., "LLL"), it returns a list of empty strings like ['', '', '', ''], and their length 0 is correctly treated as the maximum value. This makes the implementation very concise.

    Source Code

import sys

def main():
    # 入力をすべて取得し、空白(改行を含む)で分割してトークン化します
    # data[0] は試合数 N、data[1] は勝敗の文字列 S に対応します
    data = sys.stdin.read().split()
    
    # 試合の記録 S が存在するか確認します
    if len(data) < 2:
        # 文字列 S が存在しない場合は、連続勝利数は 0 とします
        if len(data) == 1:
            print(0)
        return
    
    # 勝敗を表す文字列 S を取得します
    s = data[1]
    
    # 文字列を 'L'(負け)で分割することで、'W'(勝ち)の連続部分のみを抽出します
    # 各部分の長さを map(len, ...) で計算し、その最大値を max(...) で求めます
    # 'W' が一つも含まれない場合、s.split('L') は空文字列のリストを返し、max は 0 を出力します
    print(max(map(len, s.split('L'))))

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3-flash-thinking.