Pagini recente » Cod sursa (job #1943062) | Cod sursa (job #1947507) | Cod sursa (job #2875753) | Cod sursa (job #1892492) | Cod sursa (job #745113)
Cod sursa(job #745113)
#include <stdio.h>
const int mod1 = 666013, mod2 = 100021, mod3 = 100007;
const char infile[] = "hashuri.in", outfile[] = "hashuri.out";
class hash {
private:
int H[ mod1 + 10 ];
int hash_size;
public:
hash( int size ) {
hash_size = size; }
void insert( int key );
int search( int key );
void erase( int key );
int first_hash( int key );
int second_hash( int key );
int do_hash( int key, int i );
} ;
int hash::first_hash( int key ) {
return key % mod2;
}
int hash::second_hash( int key ) {
return 1 + ( key % mod3 );
}
int hash::do_hash( int key, int i ) {
return ( first_hash( key ) + i * second_hash( key ) ) % mod1;
}
int hash::search( int key ) {
int i, j;
for( i = 0; i < mod1; ++i ) {
//j = do_hash( key, i );
j = ( ( key % mod2 ) + i * ( 1 + key % mod3 ) ) % mod1;
if( H[ j ] == 0 )
return -1;
if( H[ j ] == key )
return j;
}
return -1;
}
void hash::insert( int key ) {
int i, j;
if( search( key ) == -1 )
for( i = 0; i < mod1; ++i ) {
//j = do_hash( key, i );
j = ( ( key % mod2 ) + i * ( 1 + key % mod3 ) ) % mod1;
if( H[ j ] <= 0 ) {
H[ j ] = key;
return;
}
}
}
void hash::erase( int key ) {
int j = search( key );
if( j != -1 )
H[ j ] = -1;
}
int main() {
freopen( infile, "r", stdin );
freopen( outfile, "w", stdout );
hash O( mod1 );
int index, operation, value, T;
scanf( "%d", &T );
for( index = 1; index <= T; ++ index ) {
scanf( "%d %d", &operation, &value );
if( operation == 1 )
O.insert( value );
if( operation == 2 )
O.erase( value );
if( operation == 3 )
printf( "%d\n", O.search( value ) == -1 ? 0 : 1 );
}
return 0;
}