Problem solution · Go

Codeforces 1697D — Guess The String

Codeforces 1697D — Guess The String: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
109 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 1697D — Guess The String, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 109 lines of Go from the credited upstream file 1697D.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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1697D — Guess The String · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"os"	"sort"	"strconv") // https://space.bilibili.com/206214type interaction97 interface {	readInitData() initData97	query(request97) response97	printAnswer(answer97)} type stdIO97 struct {	in  *bufio.Reader	out *bufio.Writer} type (	initData97 struct{ n int }	request97  struct{ q []int }	response97 struct{ res string }	answer97   struct{ ans string }) func (io stdIO97) readInitData() initData97 {	in := io.in 	var n int	Fscan(in, &n) 	return initData97{n}} func (io stdIO97) query(q request97) (resp response97) {	in, out := io.in, io.out 	Fprint(out, "?")	for _, v := range q.q {		Fprint(out, " ", v)	}	Fprintln(out) 	out.Flush() 	Fscan(in, &resp.res) 	if resp.res == "0" {		panic(-1)	}	return} func (io stdIO97) printAnswer(a answer97) {	out := io.out 	Fprintln(out, "!", a.ans) 	out.Flush()} func doInteraction97(it interaction97) {	dt := it.readInitData()	n := dt.n 	getChar := func(i int) byte {		return it.query(request97{[]int{1, i + 1}}).res[0]	}	getDiff := func(l, r int) int {		v, _ := strconv.Atoi(it.query(request97{[]int{2, l + 1, r + 1}}).res)		return v	} 	ans := make([]byte, n)	defer func() { it.printAnswer(answer97{string(ans)}) }() 	type pair struct {		i int		b byte	}	pos := []pair{}	for i := range ans {		j := sort.Search(len(pos), func(j int) bool { return getDiff(pos[j].i, i) > len(pos)-j }) - 1		if j < 0 {			ans[i] = getChar(i)		} else {			ans[i] = pos[j].b			pos = append(pos[:j], pos[j+1:]...)		}		pos = append(pos, pair{i, ans[i]})	}} func run97() {	in := bufio.NewReader(os.Stdin)	out := bufio.NewWriter(os.Stdout) 	T := 1	for ; T > 0; T-- {		doInteraction97(stdIO97{in, out})	}} //func main() { run97() } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗