hihoCoder#1086 Browser Caching

原题地址

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 }
原文地址:https://www.cnblogs.com/boring09/p/4423290.html