ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络练习赛 题目5 : Browser Caching

描述

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 2
www.bing.com
www.microsoft.com
www.microsoft.com
windows.microsoft.com
www.bing.com

样例输出

Internet
Internet
Cache
Internet
Internet

这道题主要考察的逻辑能力,类似操作系统中页面置换算法的最近最久未使用LRU算法。
 1 #include<set>
 2 #include<vector>
 3 #include<iostream>
 4 #include<string>
 5 #include<iterator>
 6 using namespace std;
 7 vector<string> v1;
 8 int main() {
 9     int m,n;
10     cin>>m>>n;
11     string s;
12     for( int i = 0; i < m; i++ ) {    
13         cin>>s;
14         int size = v1.size();
15         int flag = 1;
16         vector<string>::iterator it;        
17         for( it = v1.begin(); it != v1.end(); it++ ) {        
18             if( *it == s ) {                
19                 v1.erase(it);
20                 cout<<"Cache"<<endl;
21                 flag = 0;
22                 break;
23             }            
24         }    
25         if( flag ) {            
26             if( size >= n ) {
27                 v1.erase(v1.begin());        
28             }    
29             cout<<"Internet"<<endl;            
30         }        
31         v1.push_back(s);
32     } 
33     return 0;
34 }

 

posted on 2015-09-18 21:58  马衡  阅读(164)  评论(0编辑  收藏  举报

导航