Advertisement
Val_Kir

хэш таблицы петровича

Jul 2nd, 2020
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include "iostream"
  3. #include <list>
  4. #include <iterator>
  5. #include "time.h"
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. class ListString        //класс список указателей
  11. {
  12. private:list<string*>strList;           //кол-во строк
  13.        list<string*>::iterator i;      
  14.  
  15. public:
  16.     ListString() {};
  17.  
  18.     ListString(int n, int rangeChar)
  19.     {
  20.         string str;
  21.         int len;
  22.         for (int i = 0; i < n; i++)
  23.         {
  24.             len = 1 + rand() % 10;
  25.  
  26.             for (int j = 0; j < len; j++)
  27.             {
  28.                 str += (char)('a' + rand() % rangeChar);
  29.             }
  30.  
  31.             strList.push_back(new string(str));     //добавляем в конец
  32.             str = "";
  33.         }
  34.         cout << "List generation completed!" << endl;
  35.     }
  36.  
  37.     void ofStream()
  38.     {
  39.         string* str;
  40.         i = strList.begin();
  41.  
  42.         while (i != strList.end())
  43.         {
  44.             str = *i;
  45.             cout << *str << endl;
  46.             i++;
  47.         }
  48.     }
  49.  
  50.     string getValue(int step)
  51.     {
  52.         string* str;
  53.         i = strList.begin();
  54.         advance(i, step);       //приращение итератора по смещению
  55.         str = *i;
  56.         return *str;
  57.     }
  58.  
  59.     int insert(string str, int counter)
  60.     {
  61.         int check = 0;
  62.         string* str1;
  63.         i = strList.begin();
  64.  
  65.         while (i != strList.end())
  66.         {
  67.             str1 = *i;
  68.             i++;
  69.             counter++;
  70.             if (str == *str1)
  71.             {
  72.                 check = 1;
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         if (check != 1)
  78.         {
  79.             strList.push_back(new string(str));     //добавляем в конец
  80.             //return 0;
  81.         }
  82.         return counter;
  83.     }
  84.  
  85. };
  86.  
  87. class Hashtable
  88. {
  89. private: ListString* table;
  90.  
  91. public:
  92.     void tablegenerator(ListString myList, int n, int q)
  93.     {
  94.         int hsum, counter = 0;
  95.         table = new ListString[q];
  96.         for (int i = 0; i < n; i++)
  97.         {
  98.             string str = myList.getValue(i);
  99.             hsum = 0;
  100.             for (int j = 0; j < str.length(); j++)
  101.             {
  102.                 hsum = (hsum * 31 + str[j]) % q;        //полином по схеме Горнера
  103.             }
  104.  
  105.             counter += table[hsum].insert(str, counter);
  106.         }
  107.         cout << endl << counter << endl;
  108.         for (int i = 0; i < q; i++)
  109.         {
  110.             table[i].ofStream();
  111.         }
  112.  
  113.     }
  114.  
  115. };
  116.  
  117. int main()
  118. {
  119.     srand(time(NULL));
  120.  
  121.     int n, rangeChar, q;
  122.  
  123.     cout << "Please enter a range of characters (1-26): " << endl;
  124.     cin >> rangeChar;
  125.  
  126.     cout << "Please enter the number of rows (<=100000): " << endl;
  127.     cin >> n;
  128.  
  129.     cout << "Please enter the size of the hash table" <<
  130.         "(a prime number that must be greater than the number of rows, for example, 10007, 50651 or 100003): ";
  131.     cin >> q;
  132.  
  133.     ListString myList(n, rangeChar);
  134.     myList.ofStream();
  135.  
  136.     Hashtable mytable;
  137.     mytable.tablegenerator(myList, n, q);
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement