-
Notifications
You must be signed in to change notification settings - Fork 1
/
lrucache.cpp
99 lines (86 loc) · 1.57 KB
/
lrucache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<cstdio>
#include<cassert>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
map < string, string >M;
set < pair < LL, string > >S;
map < string, LL >last;
void
print_map (void)
{
map < string, string >::iterator it;
for (it = M.begin (); it != M.end (); it++)
{
cout << it->first << " " << it->second << endl;
}
}
int
main ()
{
int N, cnt = 0, BS;
//ios_base::sync_with_stdio(0);
cin >> N;
while (N--)
{
cnt++;
string s, x;
string val;
cin >> s;
if (s == "BOUND")
cin >> BS;
if (s == "SET")
{
cin >> x >> val;
set < pair < LL, string > >::iterator its = S.begin ();
if(M.count(x)==0){
S.insert(make_pair(cnt,x));
} else {
S.erase(make_pair(last[x],x));
S.insert(make_pair(cnt,x));
}
if(S.size()>BS) {
its=S.begin();
if(last.count(its->second) && (last[its->second]==its->first)){
M.erase(its->second);
last.erase(its->second);
}
S.erase(S.begin());
}
M[x] = val;
last[x] = cnt;
}
if (s == "GET")
{
cin >> x;
if (last.count (x))
{
cout << M[x] << endl;
set < pair < LL, string > >::iterator its;
S.erase (make_pair (last[x], x));
S.insert (make_pair (cnt, x));
last[x] = cnt;
}
else
cout << "NULL" << endl;
}
if (s == "PEEK")
{
cin >> x;
if (M.count (x))
cout << M[x] << endl;
else
cout << "NULL" << endl;
}
if (s == "DUMP")
{
print_map ();
}
}
return 0;
}