【hihocoder】Browser Cache

本文介绍了一个简单的LRU缓存模拟实现方案,通过使用C++编程语言来解决hihocoder提出的Browser Caching问题。该方案利用了字符串映射来记录每个元素的最后访问时间,并在每次访问时调整缓存状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本题根据hihocoder oj提供的解题报告编写:

http://hihocoder.com/discuss/question/2154


/**
 *  @author         johnsondu
 *  @createTime     2015.09.06 15:59
 *  @problem        hihocoder: Browser Caching
 *  @url            http://hihocoder.com/contest/hiho62/problem/1
 *  @type           simple simulation of LRU cache
 */


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
using namespace std;

const int N = 20005;
int n, m;
map<string, int> lastVisit;
string a[N];

int main()
{
    scanf("%d%d", &n, &m);
    int cacheCnt = 0;
    int start = 1;

    for(int i = 1; i <= n ; i ++)
    {
        cin >> a[i];
        if(lastVisit[a[i]] >= start &&
            lastVisit[a[i]] <= i) printf("Cache\n");
        else {
            printf("Internet\n");
            cacheCnt ++;
            // 缓存已满
            if(cacheCnt > m) {
                start = start + 1;
            }
        }
        //更新最后一次访问时间
        lastVisit[a[i]] = i;
        //更新s找到最新的最早的访问记录
        while(lastVisit[a[start]] != start)
            start = start + 1;
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值