【hihoCoder】#1086: Browser Caching (微软笔试题)
2015-09-16 16:08
495 查看
#1086 : Browser Caching
时间限制:10000ms单点时限:1000ms
内存限制: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 thesize 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 InternetInternet
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
// URL的数量
int urlNum = in.nextInt();
// Cache的容量
int cacheCapacity = in.nextInt();
// 验证输入的合法性
if(cacheCapacity>0 && urlNum>0){
// 使用链式存储结构来存储URL,插入、删除操作方便
LinkedList<String> cacheList = new LinkedList<String>();
// 读取第一行,为了转到下一行
in.nextLine();
// 逐行遍历URL
for(int i=0; i<urlNum; i++){
// 读取当前URL
String url = in.nextLine();
// 判断该URL是否存在于缓存cache中
if(cacheList.contains(url)){
// 存在,则先将缓存cache中该URL的节点删除
cacheList.remove(url);
// 打印结果
System.out.println("Cache");
}
else{
// 不存在,则先判断缓存是否已满
if(cacheList.size()==cacheCapacity){
// 缓存已满,则将队首的元素弹出
cacheList.poll();
}
// 打印结果
System.out.println("Internet");
}
// 最后,将当前最新的URL入队
cacheList.offer(url);
}
}
// 关闭输入流
in.close();
}
}
相关文章推荐
- 微软无线镭射简报鲨8000激光笔记本鼠标 - (2)
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- 微软镜像下载
- 微软公布2013年必应搜索十大首页美图
- IE7降低内存和降低CPU的几个技巧
- 如何高效的使用内存
- DOS下内存的配置
- 微软Word 2007数学插件 Microsoft Math 提供下载
- XP/win2003下发现1G的内存比512M还慢的解决方法
- Enterprise Library for .NET Framework 2.0缓存使用实例
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- PowerShell实现动态获取当前脚本运行时消耗的内存
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- SQL语句实现查询SQL Server内存使用状况
- C#中缓存的基本用法总结
- 巧用微软EWF来保护系统
- C语言内存对齐实例详解
- 深入学习C语言中memset()函数的用法