dipBRUR

Smallest Cyclic Shift to obtain lexicographical smallest of

Aug 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. 4. Smallest Cyclic Shift to obtain lexicographical smallest of All possible:
  2. Consider,
  3. string "aba" then the minimum cyclic shift is 1 because by shifting the pattern
  4. by one we get "aab" which is lexicographically smallest of all possible ("aba",
  5. "aab", "baa"). For solving this problem we build the suffix automaton of
  6. "String + String". Now the problem is reduced to above problem where k = 1,
  7. solving until we get substring of length = length of given string.
  8.  
  9. string ans;
  10. int t;
  11.  
  12. int minShift(int ver, int tp=0)
  13. {
  14. if (tp==t)
  15. return st[ver].link;
  16.  
  17. for (auto it : st[ver].next)
  18. {
  19. ans += it.ff;
  20. return minShift(it.ss, tp+1);
  21. }
  22. }
  23. minShift = length(s) - minShift.
  24. ans = lexicographically shortest string
  25.  
  26. Practice Problem:
  27. UVA : 1584 - Circular Sequence
  28. UVA : 1314 - Hidden Password
  29. UVA : 719 - Glass Beads
Add Comment
Please, Sign In to add comment