Suffix automatic machines for beginners

Source: Internet
Author: User

Reference: http://blog.sina.com.cn/s/blog_7812e98601012cim.html

The suffix-based automatic machine is the same as the suffix array. It was very difficult to understand at the beginning. After learning it, I felt that I was very good. Then I was dumpfounded when I got the question.

For many stringsAlgorithmThe properties are the same. If you cannot fully understand the meaning of its construction, you can't do the problem at all, such as KMP.

Algorithm. The next function is used for matching at the beginning of learning. Later, it was found that basically the question of KMP does not involve matching.

The next value is used to determine the nature of the string. If you do not understand the next value, you will not be able to solve the problem. If you have no questions, you will be asked to output the SA array.

Or the value of the rank array will allow you to construct something else based on the values of the two arrays, And then solve the problem, such as the height array.

The same is true here. Constructing a suffix-based automatic machine does not mean that you are familiar with this algorithm. It only means that you know this algorithm!

Indicating that the current understanding of suffix-based automatic machines is limited

The description and constructor of the suffix automatic mechanism have already explained the connection above. Thanks to the blogger for sharing.

Next I will explain my understanding of the problems I encountered during my learning process:

1. Why do I need to copy the result when Len = Len + 1?

If yes, it means that this node can only have one of the above nodes, and one son in the suffix automation can have multiple fathers.

Len indicates the longest path from the root node to this location. If the current father has a different son, it indicates this son.

You can only come from this father; otherwise, no, if not, the problem may occur.

As for why we need to copy it out, this is not easy to understand. copy it out, re-set the Len value, and then point it to the original

All, pointing to the node just copied, so that the purpose is

1. copy the file to avoid missing many suffixes.

2. Move all the nodes that can be suffixed to the node before copy along the route to the new node to prevent repeated suffixes.

It's messy, because it's not quite understandable.

# Include <iostream> # include <string. h> # include <stdio. h >#include <algorithm> using namespace STD; # define maxn 10000 char STR [maxn]; struct node {node * Next [26]; node * F; int Len; node (): Len (0), F (0) {memset (next, 0, sizeof (next) ;}} re_root, * tail = & re_root; int build_suffix_array (char ch) {int temp = int (CH-'A'); node * P = new node (), * q = tail, * R; p-> Len = tail-> Len + 1; for (; Q &&! Q-> next [temp]; q = Q-> F) q-> next [temp] = P; tail = P; If (q = NULL) tail-> F = & re_root; else if (Q-> next [temp]-> Len = Q-> Len + 1) tail-> F = Q; else {r = new node (); * r = * (Q-> next [temp]); R-> Len = Q-> Len + 1; tail-> F = r; P = Q-> next [temp]; P-> F = r; (; Q & Q-> next [temp] = P; q = Q-> F) q-> next [temp] = r;} return 0;} int Pos; int triverse (node * root) {bool flag = true; For (INT I = 0; I <26; I ++) {If (root-> next [I]) {STR [POS ++] = 'A' + I; triverse (root-> next [I]); pos --; flag = false ;}} if (FLAG) {STR [POS] = 0; printf ("% s \ n", STR) ;}return 0 ;}int main () {int I, K, T; scanf ("% s", STR); for (I = 0; STR [I]; I ++) build_suffix_array (STR [I]); printf ("OK \ n"); Pos = 0; triverse (& re_root); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.