Advertisement
dipBRUR

Position of first occurrence

Aug 20th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 1.01 KB | None | 0 0
  1. 5. Position of first occurrence :
  2.                                 Given a text T and we receive request of the form : given a
  3.    pattern P we need to know the position of first occurrence of P in T.
  4.    To solve this we build the suffix automaton of the text T, we also need to add in the
  5.    preprocessing finding fpos(first position of occurrence) for all states of automaton,
  6.    such that
  7.                                fpos[curr] = len[curr] - 1
  8.                                fpos[clone] = fpos[q]
  9.    then answer to our query is simply equal to fpos[t] - P.length() + 1,
  10.    where, 't' is the state corresponding to pattern, P is the pattern.
  11.  
  12. st[curr].fpos = st[curr].len - 1;
  13. st[clone].fpos = st[q].fpos;
  14. at appropriate positions in sa_extend function.
  15.  
  16. int first_occurrene(string pattern)
  17. {
  18.     int ver = 0;
  19.     int stat = 0;
  20.     int len_P = pattern.length();
  21.     while (st < len_P)
  22.     {
  23.         ver = st[ver].next[pattern[stat]-'a'];
  24.         stat++;
  25.     }
  26.     return st[ver].fpos - len_P + 1;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement