微软2015校园招聘 技术类职位在线笔试-题目1 : Browser Caching

来源:互联网 发布:mac qq远程 外置 编辑:程序博客网 时间:2024/05/22 13:39
时间限制:1000ms
单点时限:10000ms
内存限制:256MB

描述

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
翻译

时间限制:1000ms
单点时限:10000ms
内存限制:256MB

描述

当你浏览互联网,浏览器通常会缓存一些文件,以减少从远程服务器获取他们的时间成本。让我们考虑一个简单的缓存问题。假定浏览器的缓存可以存储第m页的大小。当用户访问某些网址,浏览器会首先搜索它在缓存中。如果页面已经被缓存的浏览器会从缓存中获取,否则浏览器会从互联网上获取它,并将其存储在缓存中。当缓存已满,浏览器需要存储一个新的页面,最近最少访问的页面将被丢弃。
现在,给定一个用户的浏览历史记录,请告诉我们没有浏览器那里获取网页,从高速缓存或互联网?在开始的时候浏览器的缓存是空的。

输入

第1行:两个整数N(1<= N<=20000)和M(1<= M<=5000)。 N是网页被访问的次数,M是高速缓存大小。
行2〜N + 1:每行包含由不超过30个低级字母,数字和点(“。”),这是该页面的URL的字符串。不同的URL总是导致不同的页面。例如www.bing.com和bing.com被认为是不同的网页浏览器所。

线路1〜N:对于输入的每个网址,输出“高速缓存”或“互联网”。

提示

第一次访问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)]
括号中的数字是该页面的最后访问的时间戳。

样例输入
5 2www.bing.comwww.microsoft.comwww.microsoft.comwindows.microsoft.comwww.bing.com
样例输出
InternetInternetCacheInternetInternet


下面是我的代码

#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;//找到对应的位置计数加1cout<<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;}


0 0