题目链接:
点击打开链接
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
const int maxn = 20005;
int cache_size=0;
int n,m,k=1;
int pos[maxn];
int first = 1,last = maxn;
string s;
map<string, int> mp;
struct node{
int num,pre,nxt;
}cache_list[maxn];
bool is_full(){
if(cache_size == m) return true;
return false;
}
bool is_in(int x){
if(pos[x]<first){
return false;
}
return true;
}
void add(int x){
int p = cache_list[last].pre+1;
pos[x] = p;
cache_list[cache_list[last].pre].nxt = p;
cache_list[p].pre = cache_list[last].pre;
cache_list[p].nxt = last;
cache_list[last].pre = p;
cache_size++;
}
void del(int x){
int p = pos[x];
if(p == first){
first = cache_list[first].nxt;
}
else {
int pre = cache_list[p].pre;
int nxt = cache_list[p].nxt;
cache_list[pre].nxt = nxt;
cache_list[nxt].pre = pre;
}
cache_size--;
}
void update(int x){
del(x);
add(x);
}
int main(){
cin>>n>>m;
memset(pos,-1,sizeof(pos));
for(int i=1; i<=n; i++){
cin>>s;
if(mp.count(s) == 0){
mp[s] = k++;
}
int x = mp[s];
if(is_in(x)){
printf("Cache\n");
update(x);
}else {
printf("Internet\n");
if(is_full()){
first = cache_list[first].nxt;
cache_size--;
}
add(x);
}
}
return 0;
}