Advertisement
Guest User

adler_finish

a guest
May 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. vector <string > ciagi;
  9. vector <unsigned int> djb_tab;
  10. vector <unsigned int> adler_tab;
  11.  
  12.  
  13. unsigned int djb(string text) {
  14.  
  15. int H = 5381;
  16.  
  17. for (int i = 0; i < text.size(); i++) {
  18. H = H * 32 + H + (int)text[i];
  19. }
  20.  
  21. return H;
  22. }
  23.  
  24. unsigned int adler32(string text) {
  25. int A = 1;
  26. int B = 0;
  27. int p = 65521; // NAJWIEKSZA LICZBA PIERWSZA W ZAKRESIE DO 16 BITOW
  28. for (int i = 0; i < text.size(); i++) {
  29. A = (A + (int)text[i]) % p;
  30. B = (B + A) % p;
  31. }
  32. return B * 65536 + A; // B * 65536 -> PRZESUNIECIE O 16 BITOW
  33. }
  34.  
  35. void collision(int D, int N) {
  36.  
  37. srand(time(NULL));
  38. string ciag = "";
  39. for (int i = 0; i < N; i++) {
  40. ciag = "";
  41. for (int j = 0; j < D; j++) {
  42. ciag += static_cast<char>((rand() % 122) + 48);
  43. }
  44. ciagi.push_back(ciag);
  45. }
  46. long long K = 0;
  47.  
  48. cout << endl;
  49. int metoda;
  50. cout << "Wybierz metoda: " << endl;
  51. cout << "1. DJB" << endl;
  52. cout << "2. ADLER32" << endl;
  53. cin >> metoda;
  54. switch (metoda)
  55. {
  56. case 1:
  57.  
  58. for (int i = 0; i < ciagi.size(); i++) {
  59. djb_tab.push_back(djb(ciagi[i]));
  60. //cout << ciagi[i] << endl;
  61. }
  62. for (int i = 0; i < djb_tab.size(); i++) {
  63. for (int j = i; j < djb_tab.size(); j++) {
  64. if (djb_tab[i] == djb_tab[j] && i != j) {
  65. K++;
  66. if (K < 3) {
  67. cout << "Kolizja " << K << endl;
  68. cout << "Ciag " << i << ": " << ciagi[i] << endl;
  69. cout << "Ciag " << j << ": " << ciagi[j] << endl;
  70. cout << endl << "Suma kontrolna: " << djb_tab[i] << endl;
  71. cout << "****************************************************************************" << endl;
  72. }
  73. }
  74.  
  75. }
  76. }
  77. cout << "DJB\t" << "D = " << D << " N = " << N << " K = " << K << endl;
  78. break;
  79. case 2:
  80.  
  81. for (int i = 0; i < ciagi.size(); i++) {
  82. adler_tab.push_back(adler32(ciagi[i]));
  83. //cout << ciagi[i] << endl;
  84. }
  85. for (int i = 0; i < adler_tab.size(); i++) {
  86. for (int j = i; j < adler_tab.size(); j++) {
  87. if (adler_tab[i] == adler_tab[j] && i != j) {
  88. K++;
  89. if (K < 3) {
  90. cout << "Kolizja " << K << endl;
  91. cout << "Ciag " << i << ": " << ciagi[i] << endl;
  92. cout << "Ciag " << j << ": " << ciagi[j] << endl;
  93. cout << endl << "Suma kontrolna: " << adler_tab[i] << endl;
  94. cout << "****************************************************************************" << endl;
  95. }
  96. }
  97.  
  98. }
  99. }
  100. cout << "ADLER32\t" << "D = " << D << " N = " << N << " K = " << K << endl;
  101. break;
  102. default:
  103. break;
  104. }
  105.  
  106. }
  107. int main() {
  108.  
  109. string test = "test"; // ADLER = 73204161 & DJB = 2090756197
  110. string tset = "tset"; // ADLER = 74121665 & DJB = 209077981
  111.  
  112. //cout << "DJB: " << djb(test) << " " << djb(tset) << endl;
  113. //cout << "ADLER: " << adler32(test) << " " << adler32(tset) << endl;
  114.  
  115. collision(100, 100000);
  116. system("PAUSE");
  117. }
  118. ////// OPEN PGP STUDIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement