码迷,mamicode.com
首页 > 其他好文 > 详细

hihoCoder#1086 Browser Caching

时间:2015-04-13 22:40:15      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

list+map可以轻松搞定,如果不借助STL实现起来还是挺麻烦的

 

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <list>
 4 #include <map>
 5 
 6 using namespace std;
 7 
 8 int N, M;
 9 map<string, list<string>::iterator> record;
10 list<string> cache;
11 int size = 0;
12 
13 int main() {
14   string url;
15 
16   cin >> N >> M;
17   while (N--) {
18     cin >> url;
19     auto p = record.find(url);
20     if (p != record.end()) {
21       cache.erase(p->second);
22       cache.push_front(p->first);
23       cout << "Cache" << endl;
24     }
25     else if (size >= M){
26       record.erase(cache.back());
27       cache.pop_back();
28       cache.push_front(url);
29       record.insert(pair<string, list<string>::iterator>(url, cache.begin()));
30       cout << "Internet" << endl;
31     }
32     else {
33       cache.push_front(url);
34       size++;
35       record.insert(pair<string, list<string>::iterator>(url, cache.begin()));
36       cout << "Internet" << endl;
37     }
38   }
39   return 0;
40 }

 

hihoCoder#1086 Browser Caching

标签:

原文地址:http://www.cnblogs.com/boring09/p/4423290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!