Describe
When you browse the Internet, browser usually caches some documents to reduce the time cost of fetching them from remote s Ervers. Let ' s consider a simplified caching problem. Assume the size of browser ' s cache can store M pages. When user visits some URL, browser would search it in the cache first. If the page is already cached browser would fetch it from the cache, otherwise browser would fetch it from the Internet and Store it in the cache. When the cache was full and browser need to store a new page, the least recently visited page would be discarded.
Now, given a user's browsing the history of the US where did browser fetch the pages, from the cache or the Internet? At the beginning browser ' s cache is empty.
Input
Line 1:two integers n (1 <= n <= 20000) and M (1 <= M <= 5000). N is the number of pages visited and M are the cache size.
Line 2~n+1:each line contains a string consisting of no more than in lower letters, digits and dots ('. ') which is the URL of the page. Different URLs always leads to Different pages. For example www.bing.com and bing.com is considered as different pages by browser.
Output
Line 1~n:for each URL in the input, output "Cache" or "Internet".
Tips
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 was the last visiting timestamp of the page.
Sample input
5 2www.bing.comwww.microsoft.comwww.microsoft.comwindows.microsoft.comwww.bing.com
Sample output
Internetinternetcacheinternetinternet
This problem mainly examines the logical ability, similar to the operating system in the most recent page replacement algorithm is not using the LRU algorithm.
1#include <Set>2#include <vector>3#include <iostream>4#include <string>5#include <iterator>6 using namespacestd;7vector<string>v1;8 intMain () {9 intM,n;TenCin>>m>>N; One strings; A for(inti =0; I < m; i++ ) { -Cin>>s; - intSize =v1.size (); the intFlag =1; -vector<string>:: Iterator it; - for(it = V1.begin (); It! = V1.end (); it++ ) { - if(*it = =s) { + V1.erase (it); -cout<<"Cache"<<Endl; +Flag =0; A Break; at } - } - if(flag) { - if(Size >=N) { - v1.erase (V1.begin ()); - } incout<<"Internet"<<Endl; - } to V1.push_back (s); + } - return 0; the}
ACM-ICPC International College Student Program Design Competition Beijing (2015) Online Practice Contest Topic 5:browser Caching