package main
import (
"container/list"
"fmt"
)
type Solution struct {
Cap int
Dict map[int] *list.Element
MyList *list.List
}
type Pair struct {
key,value int
}
func Construct(capacity int) Solution{
return Solution{
capacity,
map[int] *list.Element{},
list.New(),
}
}
func (this * Solution) get(key int) int {
if res, ok := this.Dict[key]; ok {
this.MyList.MoveToFront(res)
return res.Value.(Pair).value
} else {
return -1
}
}
func (this * Solution) set(key, value int) {
//判断在不在缓存
if res, ok := this.Dict[key]; ok {
res.Value = Pair{key,value}
// this.MyList.MoveToFront(res)
// this.MyList.MoveToFront(res)
return
}
this.Dict[key] = this.MyList.PushFront(Pair{key,value})
if len(this.Dict) > this.Cap {
res := this.MyList.Remove(this.MyList.Back())
delete(this.Dict, res.(Pair).key)
}
}
func main() {
var cap int
fmt.Scan(&cap)
solution := Construct(cap)
for {
var op string
// fmt.Println(op)
_, err := fmt.Scan(&op)
// fmt.Println(op, err)
if err != nil {
break
}
var key, value int
if op == "p" {
fmt.Scan(&key, &value)
solution.set(key, value)
} else if op == "g"{
fmt.Scan(&key)
res := solution.get(key)
fmt.Println(res)
}
// fmt.Println(op, key, value)
}
}