Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int BASE1 = 29;
  6. const int BASE2 = 31;
  7. int TABLESIZE = 8;
  8. int realSize = 0;
  9.  
  10. int h1( string s )
  11. {
  12. int result = 0;
  13. for( int i = 0; i < s.length(); i++ ) {
  14. result = ( result * BASE1 + s[i] ) % TABLESIZE;
  15. }
  16. return result;
  17. }
  18.  
  19. int h2( string s )
  20. {
  21. int result = 0;
  22. for( int i = 0; i < s.length(); i++ ) {
  23. result = ( result * BASE2 + s[i] ) % TABLESIZE;
  24. }
  25. return ( result * 2 + 1 ) % TABLESIZE;
  26. }
  27.  
  28. string Find( string value, vector <string> &v, vector <bool> &deleted )
  29. {
  30. int x = h1( value );
  31. int y = h2( value );
  32. bool flag = false;
  33. for( int i = 0; i < TABLESIZE; i++ ) {
  34. if( v[x] != "#" ) {
  35. if( v[x] == value && !deleted[x] ) {
  36. return "OK";
  37. }
  38. } else {
  39. return "FAIL";
  40. }
  41. x = ( x + y ) % TABLESIZE;
  42. }
  43. return "FAIL";
  44. }
  45.  
  46. void ReSize( vector <string> &v, vector <bool> &deleted );
  47.  
  48. string Insert( string value, vector <string> &v, vector <bool> &deleted )
  49. {
  50. if( Find( value, v, deleted ) == "OK" ) {
  51. return "FAIL";
  52. }
  53.  
  54. int x = h1( value );
  55. int y = h2( value );
  56. for( int i = 0; i < TABLESIZE; i++ ) {
  57. if( deleted[x] || v[x] == "#" ) {
  58. v[x] = value;
  59. deleted[x] = false;
  60. realSize++;
  61. // если коэф. заполнения >= 3/4, то делаем перехеширование
  62. if( realSize * 4 >= TABLESIZE * 3 ) {
  63. ReSize( v, deleted );
  64. }
  65. return "OK";
  66. }
  67. x = ( x + y ) % TABLESIZE;
  68. }
  69. }
  70.  
  71. string Erase( string value, vector <string> &v, vector <bool> &deleted )
  72. {
  73. if( Find( value, v, deleted ) == "FAIL" ) {
  74. return "FAIL";
  75. }
  76.  
  77. int x = h1( value );
  78. int y = h2( value );
  79. for( int i = 0; i < TABLESIZE; i++ ) {
  80. if( v[x] != "#" ) {
  81. if( v[x] == value ) {
  82. deleted[x] = true;
  83. realSize--;
  84. }
  85. }
  86. x = ( x + y ) % TABLESIZE;
  87. }
  88.  
  89. return "OK";
  90. }
  91.  
  92. void ReSize( vector <string> &v, vector <bool> &deleted )
  93. {
  94. // создаем копии векторов v и deleted
  95. vector <string> tmpV( TABLESIZE );
  96. vector <bool> tmpDeleted( TABLESIZE );
  97. for( int i = 0; i < TABLESIZE; i++ ) {
  98. tmpV[i] = v[i];
  99. tmpDeleted[i] = deleted[i];
  100. }
  101.  
  102. TABLESIZE = TABLESIZE * 2;
  103. realSize = 0;
  104. v.resize( TABLESIZE );
  105. deleted.resize( TABLESIZE );
  106. for( int i = 0; i < TABLESIZE; i++ ) {
  107. v[i] = "#";
  108. deleted[i] = true;
  109. }
  110.  
  111. for( int i = 0; i < tmpV.size(); i++ ) {
  112. if( tmpDeleted[i] == false ) {
  113. string tmp = Insert( tmpV[i], v, deleted );
  114. }
  115. }
  116. }
  117.  
  118. int main()
  119. {
  120. vector <string> v( TABLESIZE, "#" );
  121. vector <bool> deleted( TABLESIZE, true );
  122. string operation, word;
  123.  
  124. while( cin >> operation >> word ) {
  125. if( operation == "+" ) {
  126. cout << Insert( word, v, deleted ) << endl;
  127. }
  128. if( operation == "-" ) {
  129. cout << Erase( word, v, deleted ) << endl;
  130. }
  131. if( operation == "?" ) {
  132. cout << Find( word, v, deleted ) << endl;
  133. }
  134. }
  135.  
  136. v.clear();
  137. deleted.clear();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement