- 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
- 92 lines of Go from the credited upstream file 1098B.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 CF1098B(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, m int16 Fscan(in, &n, &m)17 ans := make([][]byte, n)18 t := make([][]byte, n)19 a := make([][]byte, n)20 for i := range a {21 Fscan(in, &a[i])22 ans[i] = make([]byte, m)23 t[i] = make([]byte, m)24 }25 mi := int(1e9)26 for _, p := range []string{"AGCT", "ACGT", "ATGC", "GCAT", "GTAC", "CTAG"} {27 d := 028 for i, r := range a {29 x, y := 0, 030 for j, b := range r {31 if b != p[i&1<<1|j&1] {32 x++33 }34 if b != p[i&1<<1|j&1^1] {35 y++36 }37 }38 if x < y {39 d += x40 for j := range r {41 t[i][j] = p[i&1<<1|j&1]42 }43 } else {44 d += y45 for j := range r {46 t[i][j] = p[i&1<<1|j&1^1]47 }48 }49 }50 if d < mi {51 mi = d52 for i, r := range t {53 copy(ans[i], r)54 }55 }56 d = 057 for j := range a[0] {58 x, y := 0, 059 for i, r := range a {60 if r[j] != p[j&1<<1|i&1] {61 x++62 }63 if r[j] != p[j&1<<1|i&1^1] {64 y++65 }66 }67 if x < y {68 d += x69 for i := range t {70 t[i][j] = p[j&1<<1|i&1]71 }72 } else {73 d += y74 for i := range t {75 t[i][j] = p[j&1<<1|i&1^1]76 }77 }78 }79 if d < mi {80 mi = d81 for i, r := range t {82 copy(ans[i], r)83 }84 }85 }86 for _, r := range ans {87 Fprintf(out, "%s\n", r)88 }89}90 9192