Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int BASE1 = 29;
- const int BASE2 = 31;
- int TABLESIZE = 8;
- int realSize = 0;
- int h1( string s )
- {
- int result = 0;
- for( int i = 0; i < s.length(); i++ ) {
- result = ( result * BASE1 + s[i] ) % TABLESIZE;
- }
- return result;
- }
- int h2( string s )
- {
- int result = 0;
- for( int i = 0; i < s.length(); i++ ) {
- result = ( result * BASE2 + s[i] ) % TABLESIZE;
- }
- return ( result * 2 + 1 ) % TABLESIZE;
- }
- string Find( string value, vector <string> &v, vector <bool> &deleted )
- {
- int x = h1( value );
- int y = h2( value );
- bool flag = false;
- for( int i = 0; i < TABLESIZE; i++ ) {
- if( v[x] != "#" ) {
- if( v[x] == value && !deleted[x] ) {
- return "OK";
- }
- } else {
- return "FAIL";
- }
- x = ( x + y ) % TABLESIZE;
- }
- return "FAIL";
- }
- void ReSize( vector <string> &v, vector <bool> &deleted );
- string Insert( string value, vector <string> &v, vector <bool> &deleted )
- {
- if( Find( value, v, deleted ) == "OK" ) {
- return "FAIL";
- }
- int x = h1( value );
- int y = h2( value );
- for( int i = 0; i < TABLESIZE; i++ ) {
- if( deleted[x] || v[x] == "#" ) {
- v[x] = value;
- deleted[x] = false;
- realSize++;
- // если коэф. заполнения >= 3/4, то делаем перехеширование
- if( realSize * 4 >= TABLESIZE * 3 ) {
- ReSize( v, deleted );
- }
- return "OK";
- }
- x = ( x + y ) % TABLESIZE;
- }
- }
- string Erase( string value, vector <string> &v, vector <bool> &deleted )
- {
- if( Find( value, v, deleted ) == "FAIL" ) {
- return "FAIL";
- }
- int x = h1( value );
- int y = h2( value );
- for( int i = 0; i < TABLESIZE; i++ ) {
- if( v[x] != "#" ) {
- if( v[x] == value ) {
- deleted[x] = true;
- realSize--;
- }
- }
- x = ( x + y ) % TABLESIZE;
- }
- return "OK";
- }
- void ReSize( vector <string> &v, vector <bool> &deleted )
- {
- // создаем копии векторов v и deleted
- vector <string> tmpV( TABLESIZE );
- vector <bool> tmpDeleted( TABLESIZE );
- for( int i = 0; i < TABLESIZE; i++ ) {
- tmpV[i] = v[i];
- tmpDeleted[i] = deleted[i];
- }
- TABLESIZE = TABLESIZE * 2;
- realSize = 0;
- v.resize( TABLESIZE );
- deleted.resize( TABLESIZE );
- for( int i = 0; i < TABLESIZE; i++ ) {
- v[i] = "#";
- deleted[i] = true;
- }
- for( int i = 0; i < tmpV.size(); i++ ) {
- if( tmpDeleted[i] == false ) {
- string tmp = Insert( tmpV[i], v, deleted );
- }
- }
- }
- int main()
- {
- vector <string> v( TABLESIZE, "#" );
- vector <bool> deleted( TABLESIZE, true );
- string operation, word;
- while( cin >> operation >> word ) {
- if( operation == "+" ) {
- cout << Insert( word, v, deleted ) << endl;
- }
- if( operation == "-" ) {
- cout << Erase( word, v, deleted ) << endl;
- }
- if( operation == "?" ) {
- cout << Find( word, v, deleted ) << endl;
- }
- }
- v.clear();
- deleted.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement