Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- using namespace std;
- int ASCIIsum(string text)
- {
- int result = 0;
- for (int i = 0; i < text.length(); i++)
- result += text.at(i);
- if (result < 0 || result > 100000)
- cout << "Problem" << endl;
- return result;
- }
- int ASCIIPrimary(string text, int primary)
- {
- int result = 0;
- for (int i = 0; i < text.length(); i++)
- result += text.at(i);
- return primary - (result % primary);
- }
- int ASCIIhash(string text, int txtSize)
- {
- int hash = 5381;
- for (int i = 0; i < text.length(); i++)
- hash = (hash * 33 + text.at(i)) % txtSize;
- if (hash < 0 || hash > txtSize)
- cout << "Problem" << endl;
- return hash;
- }
- int find1(string* hashTab, int size, string text)
- {
- int hash = ASCIIsum(text) % size; //liczymy hash
- if (hashTab[hash] == text)
- return hash;
- else
- {
- int i = 1;
- do
- {
- hash = (ASCIIsum(text) + i) % size;
- i++;
- if (hashTab[hash] == text)
- return hash;
- } while (i < size);
- }
- return -1; //nie udalo sie znalezc
- }
- int find2(string* hashTab, int size, string text)
- {
- int hash = ASCIIhash(text, size); //liczymy hash
- if (hashTab[hash] == text)
- return hash;
- else
- {
- int i = 1;
- do
- {
- hash = (ASCIIhash(text, size) + i) % size;
- i++;
- if (hashTab[hash] == text)
- return hash;
- } while (i < size);
- }
- return -1; //nie udalo sie znalezc
- }
- int find3(string* hashTab, int size, string text)
- {
- int hash = ASCIIsum(text) % size; //liczymy hash
- if (hashTab[hash] == text)
- return hash;
- else
- {
- int i = 1;
- int prim = 397; //wklepmy jakis prim
- do
- {
- hash = (ASCIIsum(text) + i * ASCIIPrimary(text, prim)) % size;
- i++;
- if (hashTab[hash] == text)
- return hash;
- } while (i < size);
- }
- return -1; //nie udalo sie znalezc
- }
- int find4(string* hashTab, int size, string text)
- {
- int hash = ASCIIhash(text, size); //liczymy hash
- if (hashTab[hash] == text)
- return hash;
- else //find new place
- {
- int i = 1;
- int prim = 397;
- do
- {
- hash = (ASCIIhash(text,size) + i * ASCIIPrimary(text, prim)) % size;
- i++;
- if (hashTab[hash] == text)
- return hash;
- } while (i < size);
- }
- return -1; //nie udalo sie znalezc
- }
- int main()
- {
- ifstream file("tab.txt", ifstream::in);
- string temp;
- vector<string> txts;
- while (getline(file, temp)) //odczytujemy linie i pakujemy do vectora, dlugosc vectora == ilosc elementow
- txts.push_back(temp);
- file.close();
- string* hash1 = new string[txts.size()]();
- string* hash2 = new string[txts.size()]();
- string* hash3 = new string[txts.size()]();
- string* hash4 = new string[txts.size()]();
- for (auto text : txts)
- {
- int hash = ASCIIsum(text) % txts.size(); //liczymy hash
- if (hash1[hash] == "")
- hash1[hash] = text;
- else
- {
- int i = 1;
- do
- {
- hash = (ASCIIsum(text) + i) % txts.size();
- i++;
- } while (hash1[hash] != "");
- hash1[hash] = text;
- }
- }
- string text;
- cout << "jak chcesz szukac to podaj tekst: ";
- cin >> text;
- int index = find1(hash1, txts.size(), text);
- if (index >= 0)
- cout << "Znaleziono na miejscu: " << index << endl;
- else
- cout << "Nie znaleziono" << endl;
- for (auto text : txts)
- {
- int hash = ASCIIhash(text,txts.size()); //liczymy hash
- if (hash2[hash] == "")
- hash2[hash] = text;
- else
- {
- int i = 1;
- do
- {
- hash = (ASCIIhash(text, txts.size()) + i) % txts.size();
- i++;
- } while (hash2[hash] != "");
- hash2[hash] = text;
- }
- }
- cout << "jak chcesz szukac to podaj tekst: ";
- cin >> text;
- index = find2(hash2, txts.size(), text);
- if (index >= 0)
- cout << "Znaleziono na miejscu: " << index << endl;
- else
- cout << "Nie znaleziono" << endl;
- for (auto text : txts)
- {
- int hash = ASCIIsum(text) % txts.size();
- if (hash3[hash] == "")
- hash3[hash] = text;
- else
- {
- int i = 1;
- int prim = 397; //tu wstaw liczbe pierwsza mniejsza od txts.size()
- do
- {
- hash = (ASCIIsum(text) + i * ASCIIPrimary(text, prim)) % txts.size();
- i++;
- } while (hash3[hash] != "");
- hash3[hash] = text;
- }
- }
- cout << "jak chcesz szukac to podaj tekst: ";
- cin >> text;
- index = find3(hash3, txts.size(), text);
- if (index >= 0)
- cout << "Znaleziono na miejscu: " << index << endl;
- else
- cout << "Nie znaleziono" << endl;
- for (auto text : txts)
- {
- int hash = ASCIIhash(text, txts.size());
- if (hash4[hash] == "")
- hash4[hash] = text;
- else
- {
- int i = 1;
- int prim = 397; //tu wstaw liczbe pierwsza mniejsza od txts.size()
- do
- {
- hash = (ASCIIhash(text, txts.size()) + i * ASCIIPrimary(text, prim)) % txts.size();
- i++;
- } while (hash4[hash] != "");
- hash4[hash] = text;
- }
- }
- cout << "jak chcesz szukac to podaj tekst: ";
- cin >> text;
- index = find4(hash4, txts.size(), text);
- if (index >= 0)
- cout << "Znaleziono na miejscu: " << index << endl;
- else
- cout << "Nie znaleziono" << endl;
- delete[] hash1;
- delete[] hash2;
- delete[] hash3;
- delete[] hash4;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement