Small dishes rookie talk KMP-> Dictionary tree -> AC automaton -> trie diagram (improved and not improved)

The main purpose of this paper is to summarize their bigwigs looked for AC automata and trie view of some of the understanding and views. (Front: I is limited, summary wrong, I hope bigwigs may be noted)

----------------------------------------------- dividing line KMP -------------------------------------------------- ------------------------------------

 

Introduction: https://www.cnblogs.com/zhangtianq/p/5839909.html (KMP comprehensive explanation)

Summary: KMP for single-mode matched to the mismatch to the next array by the local complexity reached (O (n))

 

Trie ------------------------------------------------ -------------------------------------------------- ---------------------------------------

Introduction: https://blog.csdn.net/weixin_39778570/article/details/81990417

A, Schematic

Trie tree contains a collection of strings is {in, inn, int, tea, ten, to}

 

 

Also known trie prefix tree, we clearly observed may be from the root node to the marker ( black ), are set in the string, not vice versa: for example, 0-> 1-> 2 = in 

 

Second, the construction process

Suppose we want to insert the string "in". We start at the root, i.e. node 0, P = 0 we expressed. We look at P Is there a logo with edge i connected to child nodes. Without this side, so we create a new node, i.e. node No. 1, No. 1 and the node P is set to 0 sub-node number of the node, and the edge identified as i. Finally, we move to the 1st node, that is, so P = 1.

 

 

So we put the "in" characters into the i in the Trie. Then we insert characters n, P is first find a node with a number that is not marked as the n side. Or not, then a further new node 2, is set as P 1 sub-node number of the node, and identify the side is n. And then finally moved to P = 2. So we put n also inserted. Since n is "in" the last character, so we also need to P = 2 this node is marked as the end point

 

 

Now we insert the string "inn". The process is the same, from P = 0 i started looking logo for the side, this time to find the No. 1 node. So we do not need to create a new node, and move directly to the node No. 1, that is, so P = 1. Then insert the character n, is the presence of node No. 2, No. 2 to the mobile node, P = 2. Finally, insert the character is not identified as P n time n side, so the new node as the No. 3 No. 2 node's children, identified as the n side, while the serial number of the endpoint node 3:

In general, the current character string you want to insert W, from the root node to determine whether the connection W [0] side, there went down the node P, node P is determined whether or even W [1] is side, not to create a link to the node X represents P W reaches the X [1];

Third, how to use

How to check the Trie is not included in the string S, that is, the search operation mentioned before. Find actually relatively simple. As long as we start from the root node, along with the identity S [1] -> S [ 2] -> S [3] ... -> move with S [S.len], if a last successfully reached the endpoint, it shows S in the Trie tree; no way if the last, or reach a node is not the end point, it is not described S Trie tree.
For example: If you find the "te", will start from 0 through 5 and finally to 6. 6 but not the end point, so no te in Trie tree. If you are looking for is "too", will start from 0 through 5 and 9, and then found nowhere after: 9 Node not marked as o side even out. So too it is not in the Trie.

Fourth, steal template, hee hee

#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 26 + 1;

struct Trie
{
    int Next[maxn], v;
    inline void init(){
        v = 1;
        memset(Next, -1, sizeof(Next));
    }
};

struct Trie Node[1,000,000 ]; // trie most likely number of nodes, at full attention! 

int TOT = 0 ; 

void CreateTrie ( char * STR) 
{ 
    int len = strlen (STR);
     int now = 0 ;
     for ( int I = 0 ; I <len; I ++ ) {
         int IDX = STR [I] - ' A ' ;
         int NXT = the Node [now] .NEXT [IDX];
         IF (NXT == - . 1 ) { 
            NXT = ++  TOT;
            the Node [NXT] .init ();
            Node[now].Next[idx] = nxt;
        }else Node[nxt].v++;
        now = nxt;
    }
    // Node[now].v = //尾部标志
}

int FindTrie(char *str)
{
    int len = strlen(str);
    int now = 0;
    int nxt;
    for(int i=0; i<len; i++){
        int idx = str[i]-'a';
        if(The Node [now] .NEXT [IDX] = -! . 1 ) now = the Node [now] .NEXT [IDX];
         the else  return  0 ; 
    } 
    return the Node [now] .v; // returns the value returned 
}
View Code

 

AC automaton ----------------------------------------------- -------------------------------------------------- ----------------------------------------

Opening conventional AC automata: pre-skills KMP, trie (forget the pots to see a friendly face oh)

(1) Why AC automatic machine learning and must first learn KMP dictionary tree ah?

This is something we need to look at the AC automatic machine to solve any problems.

For example: Given five words: she says she shr he her, and then given a string yasherhs. I asked a total number of words that appear in this string too (really classic)

Guess you like

Origin www.cnblogs.com/shuaihui520/p/11599847.html