Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #define long long int;
  6. using namespace std;
  7. vector <vector<int>> a(1000000);
  8.  
  9. int get_hash(int x){
  10.     int nmb = 31;
  11.     int mpl;
  12.     int res = 0;
  13.     while(x > 0){
  14.         mpl %= 10;
  15.         res += mpl*nmb;
  16.         nmb *= 31;
  17.         x /= 10;
  18.     }
  19.     return res;
  20. }
  21. void insert(int x, int hash){
  22.     bool flag = 1;
  23.     if(a[hash].size() == 0){
  24.         a[hash][0] = x;
  25.         cout << 3;
  26.     }else{
  27.         for (int i = 0; i < a[hash].size(); i++){
  28.             cout << 1;
  29.             if (x == a[hash][i]);
  30.             flag = 0;
  31.             break;
  32.         }
  33.         if (flag){
  34.             a[hash][a[hash].size()] = x;
  35.         }
  36.     }
  37. }
  38.  
  39.  
  40.  
  41.  
  42. void pop(int x, int hash){
  43.     if(a[hash].size() != 0){
  44.         for (int i = 0; i < a[hash].size(); i++){
  45.             if (x == a[hash][i]){
  46.                 swap(a[hash][i], a[hash][a[hash].size()]);
  47.                 a.pop_back();
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. void exist(int x, int hash){
  56.     if (a[hash].size() != 0){
  57.         for (int i = 0; i < a[hash].size(); i++){
  58.             if(x == a[hash][i]){
  59.                 cout << "true" << endl;
  60.                 break;
  61.             }
  62.         }
  63.     }
  64.     cout << "false" << endl;
  65. }
  66.  
  67.  
  68. int main(){
  69. //    ifstream cin("");
  70. //    ofstream cout("");
  71.     string cmd;
  72.     while(cin >> cmd){
  73.         if (cmd == "insert"){
  74.            int x;
  75.            cin >> x;
  76.            int hash = get_hash(x) / 1000000;
  77.            insert(x, hash);
  78.         }
  79.         if (cmd == "delete"){
  80.             int x;
  81.             cin >> x;
  82.             int hash = get_hash(x) / 1000000;
  83.             pop(x, hash);
  84.         }
  85.         if (cmd == "exists"){
  86.             int x;
  87.             cin >> x;
  88.             int hash = get_hash(x) / 1000000;
  89.             exist(x, hash);
  90.         }
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement