A - 連続勝利の記録 / Record of Consecutive Wins Editorial by admin
gpt-5.3-codexOverview
By scanning the string \(S\) from left to right exactly once and continuously counting how many consecutive Ws there are, we can find the longest winning streak.
When an L appears, the streak is broken, so the key point is to reset the count to \(0\).
Analysis
This problem asks us to find the “maximum length of a consecutive W segment.”
The important observation is that at each position, all we need is “the current streak length” and “the maximum value so far.”
For example, consider S = "WWLWWWLW":
W→ streak \(1\) (max \(1\))W→ streak \(2\) (max \(2\))L→ reset streak to \(0\)W→ streak \(1\)W→ streak \(2\)W→ streak \(3\) (max \(3\))L→ reset streak to \(0\)W→ streak \(1\)
The final maximum is \(3\).
A naive approach of “checking all possible intervals” would be too inefficient due to the large number of intervals (worst case \(O(N^2)\) or more). Since the constraint is \(N \le 10^6\), we need to solve it with a single pass through each character (\(O(N)\)).
The following rules are sufficient for this problem:
- If the character is
W, thencurrent += 1 - If the character is
L, thencurrent = 0 - Each time, update
max_streak = max(max_streak, current)
This way, even when there are no Ws at all, max_streak remains at its initial value of \(0\), giving the correct answer.
Algorithm
max_streak = 0(maximum consecutive wins so far)current = 0(current consecutive wins)- Scan the string \(S\) from left to right:
- If the character is
W, incrementcurrentby 1 - If the character is
L, setcurrentto 0 - Update
max_streakwithmax(max_streak, current)
- If the character is
- Output
max_streakat the end
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding the input string)
Implementation Notes
Don’t forget to set
current = 0when encountering anL.Updating
max_streakonly when the character isWalso works correctly (the provided code uses this form).Since the input can be large, using
sys.stdin.readlinein Python ensures stable and fast performance.Source Code
import sys
def main():
input = sys.stdin.readline
n = int(input().strip())
s = input().strip()
max_streak = 0
current = 0
for ch in s:
if ch == 'W':
current += 1
if current > max_streak:
max_streak = current
else:
current = 0
print(max_streak)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.3-codex.
posted:
last update: