Thread: Hashing In C
View Single Post
Old Jan 9th, 2008, 6:01 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Hashing In C

Interesting! That's much, much easier to implement. That could be written in 2 minutes, whereas mine took about 20. I now feel like I'll be wasting so much time on this contest by not using the STL. I'll have to ask the judges what they want to see...

It's interesting to note that mine is about 200% faster on the file with half a million integers. I guess that's because the stl map isn't a hash table. And your code worked, but you had a mismatched bracket before the greater than sign.

Cpp Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <map>
  5.  
  6. int main() {
  7. unsigned n, x, size, mode=0;
  8.  
  9. freopen("mode.in", "rt", stdin);
  10. scanf("%d\n", &n);
  11.  
  12. std::map<int,int> counts;
  13. while (n--) {
  14. scanf("%d\n", &x);
  15. if (( size = ++counts[x]) > mode) mode = size;
  16. }
  17. printf("%d\n", mode);
  18.  
  19. return 0;
  20. }
Sane is offline   Reply With Quote