Today, after the automatic machine, from the shadow of the game that day climbed out, jerked a look really not to drop difficult, carefully look at this is not as hard as a look ...
Required algorithm: KMP, the dictionary tree (KMP I wrote, the dictionary tree is too simple, is a thought, I can teach you a picture of life)
Talk about the dictionary tree first: look at the picture
Well, the dictionary tree to see Jiangzi a picture, you if the brain is disabled can not understand it!!
Here's the point of AC automata:
Just as the next function in KMP is the same principle
Properties of the node: 26-letter pointer, fail pointer, other ...
Focus: Fail pointer, I call it a peer pointer, I'm going to teach you how to understand that the value of the next function in KMP represents the longest same prefix that precedes the current character, and you can think of a string with a prefix that does not include the last character, and the suffix is not the first character. , and the suffix matches successfully, will the prefix not be successful? so according to the next value of the jump position before the string must have occurred, if there are more, it will be from long to short once connected, you can write one yourself will find, and I put next is called the peer pointer, the current position matching success, the position of the peer pointer point must be successful.
The AC automata algorithm is to add an operation to the sibling pointer fail on the dictionary tree you are building.
To ask for a sibling pointer:
1. Initialize: NULL
2. The first node below the root node points to the root node (you're the first one to fail, and you want to match something else?) )
3. The other node's sibling pointer: find the nearest node in the peer pointer of a layer on the current one (the sibling node will be many, like next function, direct point to the long string, short time by the long Point), requires that the next layer of the peer pointer also has the current character exists, At this point, the current node's fail will only want to go to the next level of the nearest matching successful sibling node below the node .... If there is no successful match, you will end up jumping to root and then jumping to null, just like root.
The following is another piece of the sharp self-portrait of a cock Silk:
AC automatic machine really not difficult, compared to KMP, you will find in the understanding has been skilled, the rest is the organization code, just AC hdu--2222 is a good example, anyway, online code is also more, reference, and then organize their own coding style, the algorithm you pass the border, The back is on you to extrapolate. Pro, good luck to you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The Fast Learning scheme of the AC automata