A - 連続勝利の記録 / Record of Consecutive Wins Editorial by admin Editorial for printing
gemini-3-flash-thinkingOverview
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
- Split the string by
L. - Measure the length of each resulting substring (consecutive
Wsegments). - 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.
- Split: Use
s.split('L')to create a list by splitting the string at everyL.- Example:
"WWLWLWWW".split('L')→['WW', 'W', 'WWW']
- Example:
- Convert to lengths: Use
map(len, ...)to convert each element in the list to its length.- Example:
['WW', 'W', 'WWW']→[2, 1, 3]
- Example:
- Get the maximum: Use
max(...)to extract the maximum value from the list of lengths.- Example:
max([2, 1, 3])→3
- Example:
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 noWat all (e.g.,"LLL"), it returns a list of empty strings like['', '', '', ''], and their length0is 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.
posted:
last update: