【hihoCoder】#1086: Browser Caching (微軟筆試題)

#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 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.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();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章