- 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
- 62 lines of Go from the credited upstream file 896A.go.
- The implementation visibly relies on sequence storage.
- 1 loop block 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 CF896A(in io.Reader, out io.Writer) {10 const (11 s = `What are you doing at the end of the world? Are you busy? Will you save us?`12 p0 = `What are you doing while sending "`13 p1 = `"? Are you busy? Will you send "`14 p2 = `"?`15 l0 = int64(len(p0))16 l1 = int64(len(p1))17 l2 = int64(len(p2))18 )19 sz := []int64{int64(len(s))}20 for sz[len(sz)-1] < 1e18 {21 sz = append(sz, sz[len(sz)-1]*2+l0+l1+l2)22 }23 24 var T, n int25 var k int6426 f := func() byte {27 k--28 for {29 if n < len(sz) && k >= sz[n] {30 return '.'31 }32 if n == 0 {33 return s[k]34 }35 if k < l0 {36 return p0[k]37 }38 k -= l039 n--40 if n >= len(sz) || k < sz[n] {41 continue42 }43 k -= sz[n]44 if k < l1 {45 return p1[k]46 }47 k -= l148 if k < sz[n] {49 continue50 }51 k -= sz[n]52 return p2[k]53 }54 }55 for Fscan(in, &T); T > 0; T-- {56 Fscan(in, &n, &k)57 Fprintf(out, "%c", f())58 }59}60 6162