Hash list, aka hash table, hash tables. This is a magical data structure, its complexity is constant level, because I like this data structure, here is a brief introduction.
(Students who have not learned the hash table, I recommend a tutorial: http://www.cnblogs.com/jiewei915/archive/2010/08/09/1796042.html)
Let's recall the data structures we learned earlier, make a list of tables, and compare them with the hash table (c in the table is the constant):
|
Insertion time |
Delete Time |
Find time |
Complexity of programming |
Degree of information profiling |
Unordered array |
1 |
N |
N |
☆ |
☆ |
Ordered array |
N |
N |
Logn |
☆ |
☆ |
Unordered linked list |
1 |
N |
N |
☆☆☆ |
☆ |
Balance Tree |
Logn |
Logn |
Logn |
☆☆☆☆☆ |
☆ |
Hash table |
Constant |
Constant |
Constant |
☆ |
☆☆☆ |
The first four columns in this table are very well understood (the reason why the balance tree gives five stars is because I assume that the tree is good-looking, if the code is not beautiful, I am afraid that many stars are possible, even if you write the wrong is ∞ star ... ), and the fifth column novice may be unfamiliar, here to explain.
For example, if you write an unordered structure on n elements, you don't have to think about it, just save it, write an orderly structure, and just think about the size of the two elements (for example, 3<5, "AB" < "aca"), we dissect very little information about the n number itself. So, like this data structure with low profile, you don't have to think about anything, you can write code.
But the hash table is not so, we need to do more in-depth analysis of the data (such as the number is not an integer, how long the string), or your hash table may be wrong (for example, if you have negative numbers, you directly to die), complexity degradation to linear and so on (for example, most are a large prime number of K, And you just chose to let each modulo k go hash. Therefore, in order to develop the habit of analyzing data, it is necessary to know whether you can use a hash list or use a hash table in a specific topic.
All right, so much for the introduction. Finally, since I said hash table programming complexity only two stars, then the following gives my code (hash table has many kinds, I most commonly used is the large prime hash+ chain table storage weight, code simple and strong applicability):
Title: Sjtuoj 1224
Crisp version: The Code of the hash table, refreshing version is easy for beginners to mess up, so only give the class version.
Class Edition:
1, Bare hash table (the main memory is large enough, so can be too)
2, modulus of large prime + zipper (for the subject, compared to the practice of NC, thanks to HQ students can directly use the Bare hash table AC)
//bare Hash class versionclasshash{inth[4000000]; intKintNUM) {returnnum+2000000; } intNumintK) {returnK-2000000; } Public: void inch(intnum) {+ +h[k (num)];} intCountintNUM) {returnh[k (num)];};//modulus Large prime number + Zipper Table Class Editionclasshash{structHash {intnum; Hash*Next; }*h[250000],new[250000]; intL; Hash* Make (intnum) {new[l].num=num;returnnew+l++; } intKintNUM) {return(num+2000000)%249989; } Public: void inch(intnum) {Hash*u=Make (num); U->next=h[k (num)]; H[k (num)]=u; } intCountintnum) { ints=0; for(hash *u=h[k (num)];u;u=u->next)if(u->num==num) + +s; returns; }};
"Data Structure" C + + code hash list