Browser Caching

Browser Caching

题目来源:http://hihocoder.com/contest/mstest2015dec1/problem/1

时间限制: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 2
www.bing.com
www.microsoft.com
www.microsoft.com
windows.microsoft.com
www.bing.com

样例输出

Internet
Internet
Cache
Internet
Internet

        代码如下:

   
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Main
{
    public static void main(String[] args)
    {
        int n = 0;
        int m = 0;
        String s = "";
        // 包装输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String temp = "";
        try
        {
            // 读取第一行
            temp = br.readLine();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // br.readLine()将“n m”以字符串读入,因此需要再转换城数字
        n = Integer.parseInt(temp.substring(0, temp.indexOf(" ")));
        m = Integer.parseInt(temp.substring(temp.indexOf(" ") + 1,
                temp.length()));

        HashMap map = new HashMap();
        for (int i = 0; i < n; i++)
        {
            try
            {
                s = br.readLine();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 如果map为空,则将s加入map,输出Internet
            if (map.isEmpty())
            {
                map.put(s, (Integer) 1);
                System.out.println("Internet");
            }
            else
            {
                // 如果map不为空,则判断map中是否已包含s
                // 如果已包含,则将value值更新,输出Cache
                if (map.containsKey(s))
                {
                    map.put(s, map.get(s) + (Integer) 1);
                    System.out.println("Cache");
                }
                else
                {
                    // 如果map不包含s,则判断map是否已满
                    // 如果未满,则将s加入到map中,输出Internet
                    if (map.size() < m)
                    {
                        map.put(s, (Integer) 1);
                        System.out.println("Internet");
                    }
                    else
                    {
                        // 如果已满,则遍历map,找出value值最小的元素,
                        // 将此key-value删除,添加s,输出Internet
                        int min = n;
                        String st = "";
                        for (Object key : map.keySet())
                        {
                            if (min > map.get(key))
                            {
                                min = map.get(key);
                                st = (String) key;
                            }
                        }
                        @SuppressWarnings("unused")
                        Object temp1 = map.remove(st);
                        map.put(s, (Integer) 1);
                        System.out.println("Internet");
                    }
                }
            }
        }
    }
}

转载于:https://www.cnblogs.com/Coder816/p/4164727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值