Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- #define long long int;
- using namespace std;
- vector <vector<int>> a(1000000);
- int get_hash(int x){
- int nmb = 31;
- int mpl;
- int res = 0;
- while(x > 0){
- mpl %= 10;
- res += mpl*nmb;
- nmb *= 31;
- x /= 10;
- }
- return res;
- }
- void insert(int x, int hash){
- bool flag = 1;
- if(a[hash].size() == 0){
- a[hash][0] = x;
- cout << 3;
- }else{
- for (int i = 0; i < a[hash].size(); i++){
- cout << 1;
- if (x == a[hash][i]);
- flag = 0;
- break;
- }
- if (flag){
- a[hash][a[hash].size()] = x;
- }
- }
- }
- void pop(int x, int hash){
- if(a[hash].size() != 0){
- for (int i = 0; i < a[hash].size(); i++){
- if (x == a[hash][i]){
- swap(a[hash][i], a[hash][a[hash].size()]);
- a.pop_back();
- break;
- }
- }
- }
- }
- void exist(int x, int hash){
- if (a[hash].size() != 0){
- for (int i = 0; i < a[hash].size(); i++){
- if(x == a[hash][i]){
- cout << "true" << endl;
- break;
- }
- }
- }
- cout << "false" << endl;
- }
- int main(){
- // ifstream cin("");
- // ofstream cout("");
- string cmd;
- while(cin >> cmd){
- if (cmd == "insert"){
- int x;
- cin >> x;
- int hash = get_hash(x) / 1000000;
- insert(x, hash);
- }
- if (cmd == "delete"){
- int x;
- cin >> x;
- int hash = get_hash(x) / 1000000;
- pop(x, hash);
- }
- if (cmd == "exists"){
- int x;
- cin >> x;
- int hash = get_hash(x) / 1000000;
- exist(x, hash);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement