Advertisement
dipBRUR

Finding lexicographically k'th smallest substring

Aug 20th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. 3. Finding lexicographically k'th smallest substring:
  2.                                                     The number of path / substring
  3.  of a node / state of suffix automaton defines that we can obtain such number of
  4.  substring which starts with the transitions. So, at first we calculate number of
  5.  substring of all states and then do following:
  6.  
  7.  Let, there are two states which we can go from initial state. There can be two
  8.  probabilities.
  9.  1. The number of substring from start(1) is less than k. So, we can be sure that
  10.     our desired substring will not start with transition(1). So, we subtract
  11.      that number of substring from k.
  12.  2. If the number is equal or greater then we go to these state and print the
  13.     transition name. Then subtract 1 from k as we take 1 character. The process
  14.     end when k is 0.
  15.  
  16. void kLex(int ver, int k)
  17. {
  18.     if (k == 0)
  19.         return;
  20.     for (auto it : st[ver].next)
  21.     {
  22.         int tSub = d[it.ss];
  23.         if (tSub < k)           // this state node belong
  24.         {
  25.             k -= tSub;
  26.         }
  27.         else
  28.         {
  29.             cout << it.ff;
  30.             if (k == 1)     cout << endl;
  31.  
  32.             kLex(it.ss, k-1);
  33.             return;           // do nothing when return
  34.         }
  35.     }
  36. }
  37.  
  38. Practice Problem :
  39.                  Spoj : SUBLEX - Lexicographical Substring Search
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement