Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int ASCIIsum(string text)
  9. {
  10. int result = 0;
  11. for (int i = 0; i < text.length(); i++)
  12. result += text.at(i);
  13. if (result < 0 || result > 100000)
  14. cout << "Problem" << endl;
  15. return result;
  16. }
  17.  
  18. int ASCIIPrimary(string text, int primary)
  19. {
  20. int result = 0;
  21. for (int i = 0; i < text.length(); i++)
  22. result += text.at(i);
  23. return primary - (result % primary);
  24. }
  25.  
  26. int ASCIIhash(string text, int txtSize)
  27. {
  28. int hash = 5381;
  29. for (int i = 0; i < text.length(); i++)
  30. hash = (hash * 33 + text.at(i)) % txtSize;
  31. if (hash < 0 || hash > txtSize)
  32. cout << "Problem" << endl;
  33. return hash;
  34. }
  35.  
  36. int find1(string* hashTab, int size, string text)
  37. {
  38. int hash = ASCIIsum(text) % size; //liczymy hash
  39. if (hashTab[hash] == text)
  40. return hash;
  41. else
  42. {
  43. int i = 1;
  44. do
  45. {
  46. hash = (ASCIIsum(text) + i) % size;
  47. i++;
  48. if (hashTab[hash] == text)
  49. return hash;
  50. } while (i < size);
  51. }
  52. return -1; //nie udalo sie znalezc
  53. }
  54.  
  55. int find2(string* hashTab, int size, string text)
  56. {
  57. int hash = ASCIIhash(text, size); //liczymy hash
  58. if (hashTab[hash] == text)
  59. return hash;
  60. else
  61. {
  62. int i = 1;
  63. do
  64. {
  65. hash = (ASCIIhash(text, size) + i) % size;
  66. i++;
  67. if (hashTab[hash] == text)
  68. return hash;
  69. } while (i < size);
  70. }
  71. return -1; //nie udalo sie znalezc
  72. }
  73.  
  74. int find3(string* hashTab, int size, string text)
  75. {
  76. int hash = ASCIIsum(text) % size; //liczymy hash
  77. if (hashTab[hash] == text)
  78. return hash;
  79. else
  80. {
  81. int i = 1;
  82. int prim = 397; //wklepmy jakis prim
  83. do
  84. {
  85. hash = (ASCIIsum(text) + i * ASCIIPrimary(text, prim)) % size;
  86. i++;
  87. if (hashTab[hash] == text)
  88. return hash;
  89. } while (i < size);
  90. }
  91. return -1; //nie udalo sie znalezc
  92. }
  93.  
  94. int find4(string* hashTab, int size, string text)
  95. {
  96. int hash = ASCIIhash(text, size); //liczymy hash
  97. if (hashTab[hash] == text)
  98. return hash;
  99. else //find new place
  100. {
  101. int i = 1;
  102. int prim = 397;
  103. do
  104. {
  105. hash = (ASCIIhash(text,size) + i * ASCIIPrimary(text, prim)) % size;
  106. i++;
  107. if (hashTab[hash] == text)
  108. return hash;
  109. } while (i < size);
  110. }
  111. return -1; //nie udalo sie znalezc
  112. }
  113.  
  114. int main()
  115. {
  116.  
  117. ifstream file("tab.txt", ifstream::in);
  118. string temp;
  119. vector<string> txts;
  120. while (getline(file, temp)) //odczytujemy linie i pakujemy do vectora, dlugosc vectora == ilosc elementow
  121. txts.push_back(temp);
  122. file.close();
  123. string* hash1 = new string[txts.size()]();
  124. string* hash2 = new string[txts.size()]();
  125. string* hash3 = new string[txts.size()]();
  126. string* hash4 = new string[txts.size()]();
  127. for (auto text : txts)
  128. {
  129. int hash = ASCIIsum(text) % txts.size(); //liczymy hash
  130. if (hash1[hash] == "")
  131. hash1[hash] = text;
  132. else
  133. {
  134. int i = 1;
  135. do
  136. {
  137. hash = (ASCIIsum(text) + i) % txts.size();
  138. i++;
  139. } while (hash1[hash] != "");
  140. hash1[hash] = text;
  141. }
  142. }
  143. string text;
  144. cout << "jak chcesz szukac to podaj tekst: ";
  145. cin >> text;
  146. int index = find1(hash1, txts.size(), text);
  147. if (index >= 0)
  148. cout << "Znaleziono na miejscu: " << index << endl;
  149. else
  150. cout << "Nie znaleziono" << endl;
  151.  
  152. for (auto text : txts)
  153. {
  154. int hash = ASCIIhash(text,txts.size()); //liczymy hash
  155. if (hash2[hash] == "")
  156. hash2[hash] = text;
  157. else
  158. {
  159. int i = 1;
  160. do
  161. {
  162. hash = (ASCIIhash(text, txts.size()) + i) % txts.size();
  163. i++;
  164. } while (hash2[hash] != "");
  165. hash2[hash] = text;
  166. }
  167. }
  168. cout << "jak chcesz szukac to podaj tekst: ";
  169. cin >> text;
  170. index = find2(hash2, txts.size(), text);
  171. if (index >= 0)
  172. cout << "Znaleziono na miejscu: " << index << endl;
  173. else
  174. cout << "Nie znaleziono" << endl;
  175.  
  176. for (auto text : txts)
  177. {
  178. int hash = ASCIIsum(text) % txts.size();
  179. if (hash3[hash] == "")
  180. hash3[hash] = text;
  181. else
  182. {
  183. int i = 1;
  184. int prim = 397; //tu wstaw liczbe pierwsza mniejsza od txts.size()
  185. do
  186. {
  187. hash = (ASCIIsum(text) + i * ASCIIPrimary(text, prim)) % txts.size();
  188. i++;
  189. } while (hash3[hash] != "");
  190. hash3[hash] = text;
  191. }
  192. }
  193. cout << "jak chcesz szukac to podaj tekst: ";
  194. cin >> text;
  195. index = find3(hash3, txts.size(), text);
  196. if (index >= 0)
  197. cout << "Znaleziono na miejscu: " << index << endl;
  198. else
  199. cout << "Nie znaleziono" << endl;
  200. for (auto text : txts)
  201. {
  202. int hash = ASCIIhash(text, txts.size());
  203. if (hash4[hash] == "")
  204. hash4[hash] = text;
  205. else
  206. {
  207. int i = 1;
  208. int prim = 397; //tu wstaw liczbe pierwsza mniejsza od txts.size()
  209. do
  210. {
  211. hash = (ASCIIhash(text, txts.size()) + i * ASCIIPrimary(text, prim)) % txts.size();
  212. i++;
  213. } while (hash4[hash] != "");
  214. hash4[hash] = text;
  215. }
  216. }
  217. cout << "jak chcesz szukac to podaj tekst: ";
  218. cin >> text;
  219. index = find4(hash4, txts.size(), text);
  220. if (index >= 0)
  221. cout << "Znaleziono na miejscu: " << index << endl;
  222. else
  223. cout << "Nie znaleziono" << endl;
  224.  
  225. delete[] hash1;
  226. delete[] hash2;
  227. delete[] hash3;
  228. delete[] hash4;
  229. system("pause");
  230. return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement