-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.cpp
61 lines (55 loc) · 814 Bytes
/
lru.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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter length of reference string\n";
cin>>n;
cout<<"Enter string\n";
int arr[n];
vector<int> cache, temp;
int it;
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
for(int i=0; i<n; i++)
{
bool flag = true;
temp = cache;
for(int k=0; k<temp.size(); k++)
{
if(arr[i] == temp[k])
{
flag = false;
it = k;
cout<<"true";
break;
}
}
if(flag == true)
{
if(cache.size() < 3)
{
cache.push_back(arr[i]);
}
else
{
cache.erase(cache.begin());
cache.push_back(arr[i]);
}
}
else
{
cache.erase(cache.begin()+ it);
cache.push_back(arr[i]);
}
temp=cache;
for(int k=0; k<temp.size(); k++)
{
cout<<temp[k]<<" ";
}
cout<<endl;
}
return 0;
}