Advertisement
8Element

1.1

Dec 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<string>
  5. #define a 9834497
  6. using namespace std;
  7. int Getkey(string str1 , int size)
  8. {
  9. long long hash = 0;
  10. for ( int i = str1.size() - 1; i >= 0; --i)
  11. {
  12. hash += (hash * a + int(str1[i])) % size;
  13. }
  14. return hash % size;
  15. }
  16. void Qprob(int &key ,int &prob, int size)
  17. {
  18. ++prob;
  19. key = (key + prob) % size;
  20.  
  21. }
  22. int Isthere(vector<pair<string, bool>>&hash, string str3, int key, int size)
  23. {
  24. int prob = 0;
  25. while ((prob < size) && (hash[key].first != "") )
  26. {
  27. if ((hash[key].first == str3) && (hash[key].second == false))
  28. {
  29. return key;
  30. }
  31. else
  32. Qprob(key, prob, size);
  33. }
  34. return -1;
  35. }
  36. bool addElement(vector<pair<string, bool>>&hash, int key, string str2, int size ,int& realsize)
  37. {
  38. bool added = false;
  39. int prob = 0;
  40. if (Isthere(hash, str2, key, size) != -1)
  41. return false;
  42. while (!added) {
  43.  
  44. if ((hash[key].first == "") || (hash[key].second==true))
  45. {
  46. hash[key].first = str2;
  47. ++realsize;
  48. added = true;
  49. hash[key].second = false;
  50. }
  51. else
  52.  
  53. Qprob(key, prob, size);
  54. }
  55. return true;
  56. }
  57.  
  58. void newHash(int& size, int realsize ,vector<pair<string,bool>>&hash)
  59. {
  60. vector<pair<string, bool>>newhash(2 * size);
  61. int m = 2 * size;
  62. for (long long i = 0; i < m; ++i)
  63. {
  64. newhash[i].first = "";
  65. newhash[i].second = false;
  66. }
  67. int i = 0, j = 0;
  68. while ((i < size) && (j <=realsize))
  69. {
  70. if ((hash[i].first != "") && (hash[i].second == false))
  71. {
  72. int key = Getkey(hash[i].first, m);
  73. bool t= addElement(newhash, key, hash[i].first,m, j);
  74. }
  75. ++i;
  76. }
  77. hash = newhash;
  78. size *= 2;
  79.  
  80.  
  81. }
  82. bool deleteElement( vector<pair<string,bool>>&hash ,string str, int key, int size, int&realsize)
  83. {
  84. int m =Isthere(hash, str, key, size);
  85. if (m == -1)
  86. return false;
  87. else
  88. {
  89. hash[m].second = true;
  90. --realsize;
  91. return true;
  92. }
  93. }
  94.  
  95. int main()
  96. {
  97. string str;
  98. char type;
  99. int size = 8;
  100. int realsize = 0;
  101. bool t = false;
  102. int Mb = 0;
  103. vector<pair<string,bool>>Hash(size);
  104. for (int i = 0; i < size; ++i)
  105. {
  106. Hash[i].first = "";
  107. Hash[i].second = false;
  108. }
  109. while (cin >> type)
  110. {
  111. if (Mb >= 0.75)
  112.  
  113. newHash(size, realsize,Hash);
  114.  
  115.  
  116. getline(cin, str);
  117. long long m = Getkey(str,size);
  118. if (type == '+')
  119. t = addElement(Hash ,m ,str ,size ,realsize);
  120. if (type == '-')
  121. t = deleteElement(Hash ,str ,m ,size, realsize);
  122. if (type == '?') {
  123. long long k = Isthere(Hash, str, m, size);
  124. if (k == -1)
  125. cout << "FAIL" << endl;
  126. else
  127. cout << "OK" << endl;
  128. continue;
  129. }
  130. if(t)
  131. cout << "OK" << endl;
  132. else
  133. cout << "FAIL" << endl;
  134.  
  135. Mb = realsize / size;
  136.  
  137. }
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement