For problem statement at 1000-1999/1100-1199/1150-1159/1156/problemG.txt this is a correct solution, but verifier at 1000-1999/1100-1199/1150-1159/1156/verifierG.go ends with test 21 failed
input:
5
b=c#a
h=c
e=g#f
res=f#e
b=g
expected: 2
e=g#f
res=f#e
got: 2
b=g#f
res=f#b
exit status 1 can you fix the verifier? ```go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Node struct {
id int
isLeaf bool
name string
left *Node
right *Node
op byte
}
var (
nodes []*Node
leafNodes map[string]*Node
memo map[string]*Node
currentVal map[string]*Node
)
func getLeaf(name string) *Node {
if n, ok := leafNodes[name]; ok {
return n
}
n := &Node{id: len(nodes), isLeaf: true, name: name}
nodes = append(nodes, n)
leafNodes[name] = n
return n
}
func getOpNode(left *Node, op byte, right *Node) *Node {
key := fmt.Sprintf("%d,%c,%d", left.id, op, right.id)
if n, ok := memo[key]; ok {
return n
}
n := &Node{id: len(nodes), isLeaf: false, left: left, right: right, op: op}
nodes = append(nodes, n)
memo[key] = n
return n
}
func getCurrentVal(name string) *Node {
if n, ok := currentVal[name]; ok {
return n
}
return getLeaf(name)
}
func generateName(idx int) string {
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
if idx < 52 {
return string(chars[idx])
}
idx -= 52
allChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
c1 := chars[idx/62]
c2 := allChars[idx%62]
return string(c1) + string(c2)
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
return
}
nStr := strings.TrimSpace(scanner.Text())
n, _ := strconv.Atoi(nStr)
nodes = make([]*Node, 0, 2000)
leafNodes = make(map[string]*Node)
memo = make(map[string]*Node)
currentVal = make(map[string]*Node)
for i := 0; i < n; i++ {
if !scanner.Scan() {
break
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
eqIdx := strings.IndexByte(line, '=')
lvalue := line[:eqIdx]
rhs := line[eqIdx+1:]
opIdx := strings.IndexAny(rhs, "$^#&")
if opIdx == -1 {
currentVal[lvalue] = getCurrentVal(rhs)
} else {
arg1 := rhs[:opIdx]
op := rhs[opIdx]
arg2 := rhs[opIdx+1:]
leftNode := getCurrentVal(arg1)
rightNode := getCurrentVal(arg2)
currentVal[lvalue] = getOpNode(leftNode, op, rightNode)
}
}
finalNode := getCurrentVal("res")
if finalNode.isLeaf {
if finalNode.name == "res" {
fmt.Println(0)
} else {
fmt.Println(1)
fmt.Printf("res=%s\n", finalNode.name)
}
return
}
visited := make(map[int]bool)
var topo []*Node
var dfs func(n *Node)
dfs = func(n *Node) {
if n.isLeaf {
return
}
if visited[n.id] {
return
}
visited[n.id] = true
dfs(n.left)
dfs(n.right)
topo = append(topo, n)
}
dfs(finalNode)
fmt.Println(len(topo))
nodeVar := make(map[int]string)
for _, n := range leafNodes {
nodeVar[n.id] = n.name
}
freshCounter := 0
getFresh := func() string {
for {
name := generateName(freshCounter)
freshCounter++
_, isLeaf := leafNodes[name]
if !isLeaf && name != "res" {
return name
}
}
}
for i, n := range topo {
var varName string
if i == len(topo)-1 {
varName = "res"
} else {
varName = getFresh()
}
nodeVar[n.id] = varName
arg1 := nodeVar[n.left.id]
arg2 := nodeVar[n.right.id]
fmt.Printf("%s=%s%c%s\n", varName, arg1, n.op, arg2)
}
}
```