- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 80 lines of Go from the credited upstream file 1272F.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 CF1272F(in io.Reader, out io.Writer) {11 min := func(a, b int) int {12 if a > b {13 return b14 }15 return a16 }17 b2i := func(b bool) int {18 if b {19 return 120 }21 return 022 }23 24 var s, t string25 Fscan(bufio.NewReader(in), &s, &t)26 n, m := len(s), len(t)27 s += "$"28 t += "$"29 30 memo := make([][][]int, n+1)31 for i := range memo {32 memo[i] = make([][]int, m+1)33 for j := range memo[i] {34 memo[i][j] = make([]int, n+m+1)35 for k := range memo[i][j] {36 memo[i][j][k] = -137 }38 }39 }40 var f func(int, int, int) int41 f = func(i, j, k int) int {42 if k > n+m {43 return 1e944 }45 if i == n && j == m && k == 0 {46 return 047 }48 p := &memo[i][j][k]49 if *p != -1 {50 return *p51 }52 *p = 1e9 53 res := f(i+b2i(s[i] == '('), j+b2i(t[j] == '('), k+1) + 154 if k > 0 {55 res = min(res, f(i+b2i(s[i] == ')'), j+b2i(t[j] == ')'), k-1)+1)56 }57 *p = res58 return res59 }60 61 ans := []byte{}62 var makeAns func(int, int, int)63 makeAns = func(i, j, k int) {64 if i == n && j == m && k == 0 {65 return66 }67 if f(i+b2i(s[i] == '('), j+b2i(t[j] == '('), k+1)+1 == f(i, j, k) {68 ans = append(ans, '(')69 makeAns(i+b2i(s[i] == '('), j+b2i(t[j] == '('), k+1)70 } else {71 ans = append(ans, ')')72 makeAns(i+b2i(s[i] == ')'), j+b2i(t[j] == ')'), k-1)73 }74 }75 makeAns(0, 0, 0)76 Fprintf(out, "%s", ans)77}78 7980