Advertisement
Sc3ric

lab8-grisha

Jun 4th, 2023 (edited)
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. const int hashRestriction = 10;
  9.  
  10. ifstream fileToRead;
  11.  
  12. void remove(vector<string> &vec, int index)
  13. {
  14.     auto it = vec.begin();
  15.     advance(it, index);
  16.     vec.erase(it);
  17. }
  18.  
  19. void removeSames(vector<string> &words) {
  20.  
  21.     for (int i = 0; i < words.size() - 1; i++) {
  22.         for (int j = i+1; j < words.size(); j++) {
  23.             if (words[i] == words[j]) {
  24.                 remove(words, j);
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. int count(vector<string> words, string toSearch) {
  31.     int c = 0;
  32.     for (auto word : words) {
  33.         if (word == toSearch) c++;
  34.     }
  35.     return c;
  36. }
  37.  
  38. class hashTable {
  39.  
  40. private:
  41.     vector<vector<string>> words;
  42.  
  43.     int lines = hashRestriction+1;
  44.  
  45. public:
  46.    
  47.     void create() {
  48.         for (int i = 0; i < lines; i++)
  49.         {
  50.             words.push_back(vector<string>());
  51.         }
  52.     }
  53.  
  54.     void add(int hash, string word) {
  55.         int line = hash;
  56.         words[line].push_back(word);
  57.     }
  58.  
  59.     void listCollision() {
  60.         bool hasFound = false;
  61.         for (int i = 0; i < lines; i++) {
  62.             if (words[i].size() > 0) {
  63.                 vector<string> reccuring;
  64.                 for (int j = 0; j < words[i].size(); j++) {
  65.                     reccuring.push_back(words[i][j]);
  66.                 }
  67.                 removeSames(reccuring);
  68.                 if (reccuring.size() > 1) {
  69.                     hasFound = true;
  70.                     cout << "Reccuring for hash " << i << " was found:" << endl;
  71.                     for (auto word : reccuring) {
  72.                         int c = count(words[i], word);
  73.                         if (c > 1) {
  74.                             cout << "Found coinsidences of word \"" << word << "\" for this hash: " << c << endl;
  75.                         }
  76.                         else {
  77.                             cout << "Found collision for this hash: " << word << endl;
  78.                         }
  79.                     }
  80.                 }
  81.                 else {
  82.                     cout << "Hash " << i << " is only for word \"" << words[i][0] << "\"" << endl;
  83.                 }
  84.                 cout << endl;
  85.             }
  86.            
  87.         }
  88.         if (!hasFound) {
  89.             cout << "Reccuring words' hashes were not found" << endl;
  90.         }
  91.     }
  92.  
  93. };
  94.  
  95. string purify(string str) {
  96.     string word = str;
  97.     string restricted = ".,;:!?& ";
  98.     for (int i = 0; i < word.size(); i++) {
  99.         for (int j = 0; j < restricted.size(); j++) {
  100.             if (word[i] == restricted[j]) {
  101.                 word.erase(i, 1);
  102.             }
  103.         }
  104.     }
  105.  
  106.     return word;
  107. }
  108.  
  109. vector<string> reverse(vector<string> words) {
  110.     vector<string> reversed;
  111.     for (auto str : words) {
  112.         string res = "";
  113.         for (int i = str.size() - 1; i >= 0; i--) {
  114.             res += str[i];
  115.         }
  116.         reversed.push_back(res);
  117.     }
  118.     return reversed;
  119. }
  120.  
  121. vector<string> split(vector<string> text) {
  122.     vector<string> words;
  123.    
  124.     for (auto str : text) {
  125.         str += ' ';
  126.         while (str.find(' ') != string::npos) {
  127.             words.push_back(purify(str.substr(0, str.find(' '))));
  128.             str.erase(0, str.find(' ') + 1);
  129.         }
  130.     }
  131.  
  132.     return words;
  133. }
  134.  
  135. unsigned int JSHash(const std::string& str)
  136. {
  137.     unsigned int hash = 1315423911;
  138.  
  139.     for (std::size_t i = 0; i < str.length(); i++)
  140.     {
  141.         hash ^= ((hash << 5) + str[i] + (hash >> 2));
  142.     }
  143.  
  144.     return hash % hashRestriction;
  145. }
  146.  
  147. int main()
  148. {
  149.     char fnamer[1000] = "";
  150.     struct stat buff;
  151.  
  152.     do {
  153.         fileToRead.close();
  154.         cout << "Enter full path to the fileToRead" << endl;
  155.         cin >> fnamer;
  156.         fileToRead.open(fnamer);
  157.     } while (stat(fnamer, &buff) != 0 || !fileToRead);
  158.    
  159.     vector<string> dataBase;
  160.     string str;
  161.     while (getline(fileToRead, str)) {
  162.         dataBase.push_back(str);
  163.     }
  164.  
  165.     for (auto word : dataBase) {
  166.         cout << word << endl;
  167.     }
  168.  
  169.     vector<string> words = reverse(split(dataBase));
  170.    
  171.     hashTable table;
  172.     table.create();
  173.  
  174.     for (auto word : words) {
  175.         table.add(JSHash(word), word);
  176.     }
  177.  
  178.     table.listCollision();
  179.    
  180.     return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement