AcWing 840. 模拟散列表-开放寻址法-golang
原题链接
简单
作者:
一只鱼
,
2021-03-16 17:19:24
,
所有人可见
,
阅读 196
模拟散列表-开放寻址法-golang
package main
import(
"fmt"
"os"
"bufio"
"strings"
"strconv"
)
var h []int
var n int
const N = 200003
const null = 0x3f3f3f3f // 大于10^9的数
func main(){
fmt.Scanf("%d", &n)
h = make([]int, N)
for i := 0; i < N; i++{
h[i] = null
}
scan := bufio.NewScanner(os.Stdin)
for i := 0; i < n; i++{
scan.Scan()
op := strings.Split(scan.Text(), " ")
x, _ := strconv.Atoi(op[1])
k := find(x)
if op[0] == "I"{
h[k] = x
continue
}
if op[0] == "Q"{
if h[k] != null{
fmt.Println("Yes")
}else{
fmt.Println("No")
}
}
}
}
func find(x int)int{
k := (x%N+N)%N
for h[k] != null && h[k] != x{
k++
if k == N{
k = 0
}
}
return k
}