Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5. Position of first occurrence :
- Given a text T and we receive request of the form : given a
- pattern P we need to know the position of first occurrence of P in T.
- To solve this we build the suffix automaton of the text T, we also need to add in the
- preprocessing finding fpos(first position of occurrence) for all states of automaton,
- such that
- fpos[curr] = len[curr] - 1
- fpos[clone] = fpos[q]
- then answer to our query is simply equal to fpos[t] - P.length() + 1,
- where, 't' is the state corresponding to pattern, P is the pattern.
- st[curr].fpos = st[curr].len - 1;
- st[clone].fpos = st[q].fpos;
- at appropriate positions in sa_extend function.
- int first_occurrene(string pattern)
- {
- int ver = 0;
- int stat = 0;
- int len_P = pattern.length();
- while (st < len_P)
- {
- ver = st[ver].next[pattern[stat]-'a'];
- stat++;
- }
- return st[ver].fpos - len_P + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement