hihocdoer 1086 : Browser Caching

来源:互联网 发布:crm软件系统功能 编辑:程序博客网 时间:2024/06/05 01:14

点击打开链接

描述

When you browse the Internet, browser usually caches some documents to reduce the time cost of fetching them from remote servers. Let's consider a simplified caching problem. Assume the size of browser's cache can store M pages. When user visits some URL, browser will search it in the cache first. If the page is already cached browser will fetch it from the cache, otherwise browser will fetch it from the Internet and store it in the cache. When the cache is full and browser need to store a new page, the least recently visited page will be discarded.

Now, given a user's browsing history please tell us where did browser fetch the pages, from the cache or the Internet? At the beginning browser's cache is empty.

输入

Line 1: Two integers N(1 <= N <= 20000) and M(1 <= M <= 5000). N is the number of pages visited and M is the cache size.

Line 2~N+1: Each line contains a string consisting of no more than 30 lower letters, digits and dots('.') which is the URL of the page. Different URLs always lead to different pages. For example www.bing.com and bing.com are considered as different pages by browser.

输出

Line 1~N: For each URL in the input, output "Cache" or "Internet".

提示

Pages in the cache before visiting 1st URL [null, null]

Pages in the cache before visiting 2nd URL [www.bing.com(1), null]

Pages in the cache before visiting 3rd URL [www.bing.com(1), www.microsoft.com(2)]

Pages in the cache before visiting 4th URL [www.bing.com(1), www.microsoft.com(3)]

Pages in the cache before visiting 5th URL [windows.microsoft.com(4), www.microsoft.com(3)]

The number in parentheses is the last visiting timestamp of the page.

样例输入
5 2www.bing.comwww.microsoft.comwww.microsoft.comwindows.microsoft.comwww.bing.com
样例输出
InternetInternetCacheInternetInternet
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<math.h>#include<vector>#include<string>#include<sstream>#include<algorithm>#include<stack>#include<queue>#include<limits.h>#include<numeric>#include<cstring>#include<map>#include<set>using namespace std;#define eps 1e-8#define LL long longconst LL mod=12357;const int MIN= -1e3;const int MAX= 128 ;const int MAX_N= 5e3 + 200;const int MAX_M = 1e5 + 10;const int MAX_K = 1e4+10;static int N, M, K, T;map <string ,int> lastvisit;int main() {cin >> N >> M;int s = 1, cacheNum = 0;vector<string> str_in;// 0 potint to empty stringstr_in.push_back("");for(int i = 1; i<=N; ++i) {string in;cin >> in;str_in.push_back(in);}for(int i =1; i<=N; ++i) {int index = lastvisit[ str_in[i] ];if(  index != 0 && index >=s && index <=i) {cout << "Cache" << endl;}//in cache// update cacheelse {//updatecacheNum += 1;if(cacheNum > M) {// fulls ++;cacheNum --;}cout <<"Internet" << endl;}lastvisit[ str_in[i] ] = i;while(lastvisit[ str_in[s]] != s) {s ++;}} system("pause");return 0;}


0 0
原创粉丝点击