首页 > 系统服务 > 详细

Cache Missing

时间:2018-01-14 15:08:18      阅读:274      评论:0      收藏:0      [点我收藏+]

这是亚麻OA 题

// find all the N substring with only one duplicate character.
#include <iostream>     // std::cout
#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>       // std::vector
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <sys/time.h>
#include <list>

//main point : use list to store the cached elements.
//solution : using list to store the cached elements and then iterate the input sequence.
using namespace std;

int CacheMiss ( vector<int>& nums, int size){
    std::list<int> ls;  //why use list? frequently delete and insert.
    
    int missCnt =0;
    for (auto& e: nums){
        if (ls.size() < size){
            ls.push_back (e);
            missCnt ++;
            cout<< "missing "<< e << endl;
            continue;
        }

        auto itor = find(ls.begin(),ls.end(),e);
        if (itor != ls.end()){//e exist in list. hit.  delete the exist one in the list and push_back it into list again.
            ls.erase(itor);
            ls.push_back(e);
               cout<< "hiting "<< e << endl;
        }
        else {// missing     delete  the first one in the list and push_back new one into list.
            cout<< "pop out " <<ls.front();
            ls.pop_front();
            
            ls.push_back(e);
           cout<< " missing "<< e << endl;
            missCnt ++;
        }    
    }
    return missCnt;
}
int main () { //get the start time. struct timeval tv; gettimeofday(&tv,NULL); long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000; //*** call the function . vector<int> in = {1, 2, 3, 4, 5, 4, 1}; auto out = CacheMiss(in , 4); cout<< " the missing time is: " << out << endl; //*** end of the call //get the time of end. gettimeofday(&tv,NULL); long te = tv.tv_sec * 1000 + tv.tv_usec / 1000; //output the time of the running. cout<<endl<< endl<< "running tmie is : " << te - ts << endl; return 0; }

 

Cache Missing

原文:https://www.cnblogs.com/HisonSanDiego/p/8283259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!