- 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
- 56 lines of Go from the credited upstream file 2050E.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 . "fmt"5 "io"6)7 89func cf2050E(in io.Reader, out io.Writer) {10 var T int11 var a, b, c []byte12 for Fscan(in, &T); T > 0; T-- {13 Fscan(in, &a, &b, &c)14 m := len(b)15 f := make([]int, m+1)16 for j, y := range b {17 f[j+1] = f[j] + b2i50(y != c[j])18 }19 for i, x := range a {20 f[0] += b2i50(x != c[i])21 for j, y := range b {22 f[j+1] = min(f[j+1]+b2i50(x != c[i+j+1]), f[j]+b2i50(y != c[i+j+1]))23 }24 }25 Fprintln(out, f[m])26 }27}28 2930func b2i50(b bool) int { if b { return 1 }; return 0 }31 3233 34func cf2050E2(in io.Reader, out io.Writer) {35 var T int36 var a, b, c []byte37 for Fscan(in, &T); T > 0; T-- {38 Fscan(in, &a, &b, &c)39 n, m := len(a), len(b)40 f := make([][]int, n+1)41 for i := range f {42 f[i] = make([]int, m+1)43 }44 for j, y := range b {45 f[0][j+1] = f[0][j] + b2i50(y != c[j])46 }47 for i, x := range a {48 f[i+1][0] = f[i][0] + b2i50(x != c[i])49 for j, y := range b {50 f[i+1][j+1] = min(f[i][j+1]+b2i50(x != c[i+j+1]), f[i+1][j]+b2i50(y != c[i+j+1]))51 }52 }53 Fprintln(out, f[n][m])54 }55}56