Advertisement
dipBRUR

6

Nov 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. Algo : Lexicographically minimul string rotationBy |
  2. Zhou Yuan |
  3. Complexity : O(n) | void matchPattern(string text, string pattern) {
  4.  
  5. int minExp(string s) { | string concate;
  6. s = s + s; | concate = pattern + "$" + text;
  7. int i = 0, j = 1, k = 0, len = s.size(); | int lenText = text.length();
  8. while (i+k<len && j+k<len) { | int lenPat = pattern.length();
  9. if (s[i+k] == s[j+k]) k++; | VI Z = getZarr(concate);
  10. else if (s[i+k] < s[j+k]) { | for (int i=0; i<concate.length(); i++) {
  11. j = j + k + 1; | if (Z[i] == lenPat) {
  12. if (j <= i) j = i + 1; | cout << "Found at " << (i-lenPat-1) << "\n"
  13. k = 0; | }
  14. } | }
  15. else { | }
  16. i = i + k + 1; |
  17. if (i <= j) i = j + 1; | >> Manacher's Algorithm find maximum pallindrome substring
  18. k = 0; | Complexity : O(n)
  19. } |
  20. } | string preprocessManacherAlgorithm(string s) {
  21. return min(i, j); // staring index of LS string | int len = s.length();
  22. } // swap number | if (len == 0) return "^$";
  23. | string t = "^";
  24. >> Z Algorithm | for (int i=0; i<len; i++) {
  25. VI getZarr(string s) { // Z অ্যারে তৈরি | t += "#";
  26. int k, k1, left, right, len = s.length(); | t += s[i];
  27. VI arr; | }
  28. arr.pb(0); | t += "#$";
  29. left = right = 0; | return t;
  30. for (k=1; k<len; k++) { | }
  31. if (k > right) { // যদি বক্স তৈরি না হয় | int manacherAlgorithm(string s) {
  32. left = right = k; // তাহলে left ও right | string t = preprocessManacherAlgorithm(s);
  33. // একই জায়গা থেকে শুরু হবে | int n = t.length();
  34. while (right<len && s[right]==s[right-left]) { | int P[1000001*2+2] = {0};
  35. right++; // যদি মিল পাওয়া যায়, | int C = 0, R = 0;
  36. } // তাহলে right index এর মান বাড়বে । | int maxPalCenterID = 1;
  37. arr.pb(right-left); | for (int i=0; i<n-1; i++) {
  38. right--; | int ii = 2*C - i;
  39. } | P[i] = (R > i) ? min(R - i, P[ii]) : 0;
  40. else { | while (t[i + 1 + P[i]] == t[i - 1- P[i]])
  41. k1 = k-left; | P[i]++;
  42. if (arr[k1] <= right-k) | if (P[i] > P[maxPalCenterID])
  43. arr.pb(arr[k1]); | maxPalCenterID = i;
  44. else { | if (i + P[i] > R) {
  45. left = k; | C = i;
  46. while (right < len && s[right] == s[right-left]) | R = i + P[i];
  47. right++; | }
  48. arr.pb(right); | }
  49. right--; | return P[maxPalCenterID];
  50. } | }
  51. } |
  52. } |
  53. return arr; |
  54. } |
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement