- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 72 lines of Go from the credited upstream file 1082B.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF1082B(in io.Reader, out io.Writer) {11 var n, g, cur, pre, ans int12 var s string13 Fscan(bufio.NewReader(in), &n, &s)14 for _, c := range s {15 if c == 'G' {16 g++17 cur++18 } else {19 pre = cur20 cur = 021 }22 if pre+cur+1 > ans {23 ans = pre + cur + 124 }25 }26 if g < ans {27 ans = g28 }29 Fprint(out, ans)30}31 3233func CF1082B2(in io.Reader, out io.Writer) {34 max := func(a, b int) int {35 if b > a {36 return b37 }38 return a39 }40 var n, c, ans int41 var s string42 Fscan(bufio.NewReader(in), &n, &s)43 44 s = "S" + s45 a := []int{}46 for i := range s {47 c++48 if i == len(s)-1 || s[i] != s[i+1] {49 if s[i] == 'G' {50 ans = max(ans, c)51 }52 a = append(a, c)53 c = 054 }55 }56 57 for i, n := 3, len(a); i < n; i += 2 {58 if a[i-1] == 1 {59 s := a[i-2] + a[i]60 if n >= 6 {61 s++62 }63 ans = max(ans, s)64 } else {65 ans = max(ans, max(a[i], a[i-2])+1)66 }67 }68 Fprint(out, ans)69}70 7172