-
5 2 www.bing.com www.microsoft.com www.microsoft.com windows.microsoft.com www.bing.com
-
5 2 www.bing.com www.microsoft.com www.microsoft.com windows.microsoft.com www.bing.com
样例输出
-
Internet Internet Cache Internet Internet
下面是我的代码
#include<iostream> #include<string> #include <vector> #include <algorithm> using namespace std; int main() { int page_num,caoch_size,i; vector<int> page_count; vector<string> page_str; cin>>page_num>>caoch_size; string *page=new string[page_num]; for(i=0;i<page_num;i++) cin>>page[i]; string out1="Internet"; string out2="Cache"; vector<string>::iterator it1; vector<int>::iterator it2; for(i=0;i<page_num;i++) { if(page_str.empty())//缓存中无网页 { page_str.push_back(page[i]); page_count.push_back(1); cout<<out1<<endl; continue;//结束本次循环 } if(page_str.size()!=caoch_size)//缓存中还没满 { vector<string>::iterator result = find( page_str.begin(), page_str.end(),page[i]); //查找是否出现过 if(result==page_str.end())//缓存中没有 { page_str.push_back(page[i]); page_count.push_back(1); cout<<out1<<endl; } else//缓存中存在 { for(it1=page_str.begin(),it2=page_count.begin();it1!=result;++it1,++it2);//找到缓存中存放些网页的地址 *it2+=1;//找到对应的位置计数加1 cout<<out2<<endl; } } else//缓存中已满 { //先查缓存中是否存在网页 vector<string>::iterator result = find(page_str.begin(),page_str.end(),page[i]); //查找是否出现过 if(result!=page_str.end())//缓存中已存在 { for(it1=page_str.begin(),it2=page_count.begin();it1!=result;++it1,++it2);//找到缓存中存放些网页的地址 *it2+=1;//对应的位置计数加1 cout<<out2<<endl; } else//缓存中不存在 { ///从后面开始查找 it1=page_str.end(); it2=page_count.end(); for(--it1,--it2;it1!=page_str.begin();--it1,--it2)//找到缓存中存放些网页的地址 { if(*it2==1)//找到第1个只保留一次的位置 { page_str.erase(it1);//把原来的删除 page_count.erase(it2); page_str.push_back(page[i]); page_count.push_back(1); cout<<out1<<endl; break; } } ///判断开头的位置 if(*it2==1)//找到第1个只保留一次的位置 { page_str.erase(it1);//把原来的删除 page_count.erase(it2); page_str.push_back(page[i]); page_count.push_back(1); cout<<out1<<endl; } else cout<<out1<<endl;///输入但不保存到缓存中 } } } delete []page; system("pause"); return 0; }
描述
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.
样例输出
Internet Internet Cache Internet Internet翻译
描述
现在,给定一个用户的浏览历史记录,请告诉我们没有浏览器那里获取网页,从高速缓存或互联网?在开始的时候浏览器的缓存是空的。
输入
行2〜N + 1:每行包含由不超过30个低级字母,数字和点(“。”),这是该页面的URL的字符串。不同的URL总是导致不同的页面。例如www.bing.com和bing.com被认为是不同的网页浏览器所。
输出
提示
第一次访问URL之前的网页缓存中的[NULL,NULL]第二次访问URL之前的页面高速缓存中的[www.bing.com(1),空]
第三次访问URL之前的页面高速缓存中的[www.bing.com(1),www.microsoft.com(2)]
第四次访问URL页面之前在缓存[www.bing.com(1),www.microsoft.com(3)]
第五次访问之前的页面高速缓存中的[windows.microsoft.com(4),www.microsoft.com(3)]
括号中的数字是该页面的最后访问的时间戳。
样例输入