Advertisement
dipBRUR

16

Nov 19th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. // Algo : KMP
  2. char text[MAXN], pat[MAXN];
  3. VI LPS(int m) {
  4. VI lps; lps.PB(0);
  5. int i = 1, j = 0;
  6. while (i < m) {
  7. if (pat[i] == pat[j]) {
  8. i++; j++;
  9. lps.PB(j);
  10. }
  11. else {
  12. if (j)
  13. j = lps[j-1];
  14. else {
  15. i++;
  16. lps.PB(0);
  17. }
  18. }
  19. }
  20. return lps;
  21. }
  22. // Count match
  23. int GET_MIN_K(int n, int m) {
  24. VI lps = LPS(m);
  25. int i = 0, j = 0, counter = 0;
  26. while (i < n) {
  27. if (text[i] == pat[j]) {
  28. i++;
  29. j++;
  30. }
  31. if (j == m) {
  32. counter++;
  33. j = lps[j-1];
  34. }
  35. else if (i < n && text[i] != pat[j]) {
  36. if (j)
  37. j = lps[j-1];
  38. else
  39. i++;
  40. }
  41. }
  42. return counter;
  43. }
  44.  
  45. Application :
  46. 1. Count total matching : concate = pat + "$" + text
  47. 2. Smallest period : d = pat.length() - lps[pat.length()-1]
  48. return (pat.length()%d == 0) ? d : len
  49.  
  50. // Algo : Z Algorithm
  51.  
  52. VI getZarr(string s) { // Z অ্যারে তৈরি
  53. int len = s.length();
  54. VI zArr;
  55. zArr.pb(0);
  56. int left = 0, right = 0;
  57. for (int k = 1; k < len; k++) {
  58. if (k > right) { // যদি বক্স তৈরি না হয়
  59. left = right = k; // তাহলে left ও right একই জায়গা থেকে শুরু হবে
  60. while (right < len && s[right] == s[right-left]) {
  61. right++; // যদি মিল পাওয়া যায়, তাহলে right index এর মান বাড়বে ।
  62. }
  63. zArr.pb(right - left);
  64. right--;
  65. }
  66. else {
  67. int k1 = k - left;
  68. if (zArr[k1] <= right-k) {
  69. zArr.pb(zArr[k1]);
  70. }
  71. else {
  72. left = k;
  73. while (right < len && s[right] == s[right-left]) {
  74. right++;
  75. }
  76. zArr.pb(right);
  77. right--;
  78. }
  79. }
  80. }
  81. return zArr;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement