我想要一个接收到hit
的 s 参数object
,显示它的频率。并且能够拥有最频繁的 top hit
s object
。
Unordered_map
适合第一部分,object
作为键和hit
值。
unordered_map<object,int>
它可以快速搜索object
并增加其hit
. 但是排序呢?priority_queue
启用顶部命中对象。但是如何增加对象的命中?
我想要一个接收到hit
的 s 参数object
,显示它的频率。并且能够拥有最频繁的 top hit
s object
。
Unordered_map
适合第一部分,object
作为键和hit
值。
unordered_map<object,int>
它可以快速搜索object
并增加其hit
. 但是排序呢?priority_queue
启用顶部命中对象。但是如何增加对象的命中?
I managed to solved it by keeping track of sorted list of objects by their hit number as I insert the objects. So there is always the list of the most N top hits. There are 3,000,000 objects and I want to have the top 20.
Here are the structures I used:
key_hit
to keep track of hits (by key, a string, I mean the object):
unordered_map<string, int> key_hit;
two arrays : hits[N]
, keys[N]
which contains the top hits and their corresponding key (object).
idx, hits, keys
0, 212, x
1, 200, y
...
N, 12, z
and another map key_idx
to keep the key and its corresponding index:
unordered_map<string,int> key_idx;
Algorithm (without details):
key
is input.key
in key_hit
, find its hit and increment (this is fast enough).hit<hits[N]
, ignore it.idx=key_idx[key]
, (if not found, add it to structures and delete the existing one. it too long to write all details)H=h[idx]++
h[idx-1]<H
. if yes, swap idx and idx-1 in key_idx
,hits
,keys
.I tried to make it fast. but I don't know how far it's fast.