- 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
- 63 lines of Go from the credited upstream file 910C.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 9func perm910C(arr []int, i int, do func([]int)) {10 if i == len(arr) {11 do(arr)12 return13 }14 perm910C(arr, i+1, do)15 for j := i + 1; j < len(arr); j++ {16 arr[i], arr[j] = arr[j], arr[i]17 perm910C(arr, i+1, do)18 arr[i], arr[j] = arr[j], arr[i]19 }20}21 22func Sol910C(reader io.Reader, writer io.Writer) {23 in := bufio.NewReader(reader)24 out := bufio.NewWriter(writer)25 defer out.Flush()26 27 pow10 := [6]int{1, 1e1, 1e2, 1e3, 1e4, 1e5}28 29 var n int30 var coefs [10]int31 var mustNotZero [10]bool32 for Fscan(in, &n); n > 0; n-- {33 var s string34 Fscan(in, &s)35 mustNotZero[s[0]-'a'] = true36 for i, c := range s {37 coefs[c-'a'] += pow10[len(s)-1-i]38 }39 }40 41 minVal := 1 << 3042 perm910C([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 0, func(digits []int) {43 val := 044 for i, d := range digits {45 if d == 0 {46 if mustNotZero[i] {47 return48 }49 continue50 }51 val += coefs[i] * d52 }53 if val < minVal {54 minVal = val55 }56 })57 Fprintln(out, minVal)58}59 60616263