【hihocoder】Browser Cache

来源:互联网 发布:淘宝超出七天退换货 编辑:程序博客网 时间:2024/06/10 20:24

本题根据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;}


0 0
原创粉丝点击