Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6.  
  7. int rank(long hash) {
  8. int r = 1;
  9. while((hash & 1) == 0 && r <=32) {
  10. ++r;
  11. hash >>= 1;
  12. }
  13. return r;
  14. }
  15.  
  16. std::pair<uint32_t, uint32_t> get_hashes(const std::string &id, std::hash<std::string> hash) {
  17. uint32_t mask_left = static_cast<uint32_t>(((1 << 16) - 1) << 16);
  18. uint32_t mask_right = (1 << 16) - 1;
  19. uint32_t buf = static_cast<uint32_t>(hash(id));
  20. return std::make_pair(((buf & mask_left) >> 16), buf & mask_right);
  21. }
  22.  
  23. uint32_t count(std::vector<int> &M) {
  24. uint32_t m = static_cast<uint32_t>(std::pow(2, 16));
  25. double alpha = 0.7213 / (1 + (1.079 / m));
  26.  
  27. double E = 0;
  28. for(size_t i = 0; i < M.size(); i++) {
  29. E += std::pow(2, M[i]);
  30. }
  31. E = alpha * m * m * std::pow(E, -1);
  32.  
  33.  
  34. double buf1 = m * 5.0 / 2;
  35. double buf2 = std::pow(2, 32) / 30.0;
  36.  
  37. if (E <= buf1) {
  38. double zero_count = 0;
  39. for (size_t i = 0; i < M.size(); i++) {
  40. if (M[i] == 0) {
  41. zero_count++;
  42. }
  43. }
  44. if (zero_count != 0) {
  45. E = m * log(m / zero_count);
  46. }
  47. } else if (E > buf2) {
  48. E = -std::pow(2, 32) * log(1 - (E / std::pow(2, 32)));
  49. }
  50.  
  51. return uint32_t(E);
  52. }
  53.  
  54. int main() {
  55. std::string line;
  56. std::hash<std::string> hash;
  57. std::vector<int> M(static_cast<int>(std::pow(2, 16)), 0);
  58. // std::ifstream in("input.txt");
  59. while (std::cin >> line)
  60. {
  61. M[get_hashes(line, hash).first] = std::max(M[get_hashes(line, hash).first], __builtin_ffs(get_hashes(line, hash).second));
  62. }
  63. uint32_t unique = count(M);
  64. std::cout << unique << std::endl;
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement