← Home
For problem statement at 0-999/500-599/560-569/562/problemF.txt this is a correct solution, but verifier at 0-999/500-599/560-569/562/verifierF.go ends with All tests passed can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type List struct {
	head int32
	tail int32
	size int32
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 10*1024*1024)
	scanner.Split(bufio.ScanWords)

	if !scanner.Scan() {
		return
	}
	n, _ := strconv.Atoi(scanner.Text())

	capNodes := 800005
	children := make([][26]int32, 1, capNodes)
	parent := make([]int32, 1, capNodes)
	depth := make([]int32, 1, capNodes)
	A_list := make([]List, 1, capNodes)
	B_list := make([]List, 1, capNodes)

	nextA := make([]int32, n+1)
	nextB := make([]int32, n+1)

	for i := 1; i <= n; i++ {
		scanner.Scan()
		s := scanner.Bytes()
		var u int32 = 0
		for _, c := range s {
			idx := c - 'a'
			if children[u][idx] == 0 {
				newNode := int32(len(children))
				children = append(children, [26]int32{})
				parent = append(parent, u)
				depth = append(depth, depth[u]+1)
				A_list = append(A_list, List{})
				B_list = append(B_list, List{})
				children[u][idx] = newNode
			}
			u = children[u][idx]
		}
		if A_list[u].size == 0 {
			A_list[u].head = int32(i)
			A_list[u].tail = int32(i)
		} else {
			nextA[A_list[u].tail] = int32(i)
			A_list[u].tail = int32(i)
		}
		A_list[u].size++
	}

	for i := 1; i <= n; i++ {
		scanner.Scan()
		s := scanner.Bytes()
		var u int32 = 0
		for _, c := range s {
			idx := c - 'a'
			if children[u][idx] == 0 {
				newNode := int32(len(children))
				children = append(children, [26]int32{})
				parent = append(parent, u)
				depth = append(depth, depth[u]+1)
				A_list = append(A_list, List{})
				B_list = append(B_list, List{})
				children[u][idx] = newNode
			}
			u = children[u][idx]
		}
		if B_list[u].size == 0 {
			B_list[u].head = int32(i)
			B_list[u].tail = int32(i)
		} else {
			nextB[B_list[u].tail] = int32(i)
			B_list[u].tail = int32(i)
		}
		B_list[u].size++
	}

	var totalScore int64
	ansA := make([]int32, 0, n)
	ansB := make([]int32, 0, n)

	for i := len(children) - 1; i >= 0; i-- {
		for A_list[i].size > 0 && B_list[i].size > 0 {
			a := A_list[i].head
			b := B_list[i].head
			ansA = append(ansA, a)
			ansB = append(ansB, b)
			totalScore += int64(depth[i])

			A_list[i].head = nextA[a]
			A_list[i].size--
			B_list[i].head = nextB[b]
			B_list[i].size--
		}

		if i > 0 {
			p := parent[i]
			if A_list[i].size > 0 {
				if A_list[p].size == 0 {
					A_list[p] = A_list[i]
				} else {
					nextA[A_list[p].tail] = A_list[i].head
					A_list[p].tail = A_list[i].tail
					A_list[p].size += A_list[i].size
				}
			}
			if B_list[i].size > 0 {
				if B_list[p].size == 0 {
					B_list[p] = B_list[i]
				} else {
					nextB[B_list[p].tail] = B_list[i].head
					B_list[p].tail = B_list[i].tail
					B_list[p].size += B_list[i].size
				}
			}
		}
	}

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fprintln(out, totalScore)
	for i := 0; i < len(ansA); i++ {
		fmt.Fprintf(out, "%d %d\n", ansA[i], ansB[i])
	}
}