mostlabs

cakod 2/1

Dec 29th, 2020 (edited)
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include <string>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <ctime>
  8.  
  9. #define d 256
  10.  
  11. using namespace std;
  12.  
  13. vector<int> prefix_function(const string& s) {//Boyer-Moore Algoritması
  14. vector<int> pi(s.length(), 0);
  15.  
  16. clock_t start = 0, end = 0;
  17.  
  18. start = clock();
  19. for (size_t i = 0; i < 1000; i++) {
  20.  
  21. for (int i = 1; i < s.length(); i++) {//pi onu hersayının kendini tekrar edisi
  22. //eger iki sayi varsa o zaman pi artmaya baslar
  23. //000120 gibi tekrar sayılarına bakar en bastan baslar
  24. int j = pi[i - 1];
  25.  
  26. while (j > 0 && s[i] != s[j]) {
  27. j = pi[j - 1];
  28. }
  29.  
  30. if (s[i] == s[j]) {
  31. pi[i] = j + 1;
  32. }
  33. else {
  34. pi[i] = j;
  35. }
  36. }
  37. }
  38.  
  39. end = clock();
  40. cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
  41. return pi;
  42. }
  43.  
  44. //*******************************************************************************************
  45. void search(char pat[], char txt[], int q)//Rabin-Karp String
  46. {
  47. int M = strlen(pat);
  48. int N = strlen(txt);
  49. int i, j;
  50. int p = 0; //karma deger pat
  51. int t = 0; // karma deger txt
  52. int h = 1;
  53.  
  54. ///bu bulmak için "pow(d, M-1)%q"
  55. for (i = 0; i < M - 1; i++)
  56. h = (h * d) % q;
  57.  
  58. //ilk yazı değerini hesaplama
  59. for (i = 0; i < M; i++)
  60. {
  61. p = (d * p + pat[i]) % q;
  62. t = (d * t + txt[i]) % q;
  63. }
  64. //deseni metinde tek tek kaydırmak için kullanılan denklem
  65. clock_t start = 0, end = 0;
  66.  
  67. start = clock();
  68. for (int r = 0; r < 1000000; r++) {
  69. if (r == 999999 ) {
  70. for (i = 0; i <= N - M; i++)
  71. {
  72.  
  73.  
  74. if (p == t)
  75. {
  76.  
  77. for (j = 0; j < M; j++)
  78. {
  79. if (txt[i + j] != pat[j])
  80. break;
  81. }
  82.  
  83. //eger p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
  84. if (j == M)
  85. cout << "sirasi: " << i << endl;
  86. }
  87.  
  88. //önde gelen rakamı sonrayı ekleme
  89. if (i < N - M)
  90. {
  91. t = (d * (t - txt[i] * h) + txt[i + M]) % q;
  92.  
  93.  
  94. if (t < 0)
  95. t = (t + q);
  96. }
  97. }
  98. }
  99. }
  100. end = clock();
  101. cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
  102. }
  103. /**********************************************************************************************/
  104. int KMPSearch(char* string, char* substring) { //Knuth–Morris–Pratt Algorithm
  105. int sl, ssl;
  106. int res = -1;
  107. sl = strlen(string);
  108. ssl = strlen(substring);
  109.  
  110. clock_t start = 0, end = 0;
  111.  
  112. start = clock();
  113. for (size_t r = 0; r < 10000; r++) {
  114. if (sl == 0) //satir dogrulama
  115. cout << "Неверно задана строка\n";
  116. else if (ssl == 0) //dogrulama
  117. cout << "Неверно задана подстрока\n";
  118. else {
  119. int i, j = 0, k = -1;
  120. int* w;
  121. w = new int[1000]; //yeni massiv yaratma
  122. w[0] = -1;
  123. while (j < ssl - 1) {
  124. while (k >= 0 && substring[j] != substring[k]) /*kadar k daha büyük veya eşit 0 ve J
  125. - thy eleman alt dize eşit değil k - Tom atamak k - thy eleman dinamik bir dizi */
  126. k = w[k];
  127. j++;
  128. k++;
  129. if (substring[j] == substring[k])
  130. w[j] = w[k];
  131. else
  132. w[j] = k;
  133. }
  134. i = 0;
  135. j = 0; //sıfırlandı
  136. while (j < ssl && i < sl) {
  137. while (j >= 0 && string[i] != substring[j])
  138. j = w[j];
  139. i++;
  140. j++;
  141. }
  142.  
  143.  
  144. delete[] w;
  145. res = j == ssl ? i - ssl : -1; //sonuç, j = ssl ve -1 ise i-ssl olacaktır
  146. }
  147. }
  148.  
  149. end = clock();
  150. cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
  151.  
  152. return res;
  153. }
  154.  
  155. int main() {
  156. string s, t;
  157. cin >> s >> t;
  158.  
  159. vector<int> pi = prefix_function(t + '#' + s);
  160.  
  161. int t_len = t.length();
  162.  
  163. for (int i = 0; i < s.length(); i++) {
  164. if (pi[t_len + 1 + i] == t_len) {
  165. cout << "s[" << i - t_len + 1 << ".." << i << "] = t" << endl;
  166. }
  167. }
  168.  
  169. cout << "********************************************************************************************" << endl;
  170. char txt[] = "qwertyxvbalpayqwefnvfx";
  171. char pat[] = "alpay";
  172.  
  173. //prime sayısı
  174. int q = 101;
  175.  
  176. search(pat, txt, q);
  177.  
  178. cout << "********************************************************************************************" << endl;
  179.  
  180. char alp[] = "qwertyxvbalpayqwefnvfx";
  181. char mal[] = "alpay";
  182. cout << KMPSearch(alp, mal);
  183.  
  184.  
  185. return 0;
  186.  
  187. }
Add Comment
Please, Sign In to add comment