Data structure beginner, algorithm and the linear form

A. Mind FIG.

II. Notes of important concepts

1, the algorithm

  1. Evaluation of the merits of the algorithm: time complexity (T (n) = O ((n)), n is the problem size f) and space complexity (change the size of the additional storage space occupied with the problem)
  2. The time complexity of the stack is calculated: 1 min 2. The special cases and the general computing module is split into n

2, ordinary linear form

  1. Head and tail interpolation interpolation

    Note: The order of the elements of the input element order to establish the first interpolation table opposite (reverse linked list can be used)

    s->next=L->next;
    L->next=s;
    
    r->next=s;
    r=s;
    
  2. For insertion of a doubly linked list

    1. Note that the precursor and the rear drive disconnect

  3. Ordered merge list (with L1, L2, L3 list)

    1. L1 and L2 point to establish the current node pointer to the elements L1 and L2 are compared, and the pointer moves to the copy node L3. If L1 or L2 is whoever is empty, the next point to the remainder L3 current pointer linked list

3, the queue stack

  1. Stack: LIFO stack of insertion and deletion in the top of the stack, to determine whether the insertion of the stack is full, delete To determine whether an empty stack

  2. Queue: FIFO queue is inserted in the tail, remove the head of the queue, inserted to determine whether a full team, remove the team to determine whether the air

  3. Circular queue: Air Force:

    r->rear==r->front;
    

    Full team:

    (r->rear+1)%r->maxsize==r->front;
    

4, the string

  1. Operation target string is a string of whole

  2. Each element is a character

  3. String pattern matching: BF algorithm, KMP algorithm (next function, nextval function)

    1. For the calculation of the next function: when j from the beginning, next [1] = 0, [j] = k, k-1 = length (largest prefix and postfix strings of equal length) next, otherwise next [j ] = 1; when j from 0, next [0] = - 1, next [1] = 0, next [j] = k, k = length (equal to the maximum length of the string prefix and postfix)

    2. For the calculation nextval function: when j starting from 1, nextval [1] = 0; for j from 0, nextval [j] = - 1,

5, the generalized table array

  1. Amn address matrix is located in row i and column j element row priority: first address + (i m + j) k (k is a storage space occupied by a single element)

    Priority column: first address + (J n-I +) (k storage space occupied by a single element) k

Third, difficult problems and solutions

  1. Difficult problems: for programming and understanding next nextval arrays and arrays (solution: case study and drawing)

    void get_next(string t, int len, int next[])
    {
        int i = 0, j = 0;//j用来控制前缀串,i用来控制后缀串
        next[0] = -1;
        while (i < len - 1) {//注意此时合法的最大下标为len-1
            if (j = -1 || t[i] == t[j]) {
                i++;
                j++;
                next[i]=j;//对相等的最长的前缀串和后缀串长度+1
            }
            else
            {
                j = next[j];//对j的回溯
            }
        }
    }
    
    j 0 1 2 3 4
    T[j] a c a c b
    Next[j] -1 0 0 1 2
    1. 设next[j]=k

    2. a [0] = - 1.i = 1, j = 0, if i and j are equal, i ++, j ++, next [i] = j, i.e., k = k + 1; j back to the contrary Next [j] . If j is -1, i is moved to i = i + 1, j legitimate back position, corresponding to the search of the longest prefix string is equal to the suffix string starting position backwards to the prefix string starting Back to the position 0; Comparative equal to the current prefix and postfix string at this time, if we get, i ++, j ++, next [i] = j, i.e., k = k + 1; j back to the contrary Next [j]

void get_nextval(string t, int len, int next[])
{
    int i = 0, j = 0;
    next[0] = -1;
    while (i < len - 1) {
        if (j = -1 || t[i] == t[j]) {
            i++;
            j++;
            if (t[i] != t[j]) {
                nextval[i] = j;
            }
            else {
                nextval[i] = nextval[j];//与next[j]求解中不同的部分
            }

        }
        else
        {
            j = nextval[j];
        }
    }
}
j 0 1 2 3
T[j] a a a b
Next[j] -1 0 1 2
Nextval[j] -1 -1 -1 2
  1. nextval [j] = - 1, if our main string is s string: aaaaaab, index represented by i, when the ratio of the s [2] and t [2] when, i ++, j ++, if s [. 3] and t [3] are equal when if nextval [i] = nextval [j], and since we mismatch string matching string and the main mode, we have to go back to the nextval j [j] is compared with s [i], the rear as an example, if a next array, need to s [3] and t [2] comparison, but with t [1], t [0] comparison, when the direct s [3] and t [0] Comparative reduce j number of backtracking. tag corresponding to the pattern string equal elements adjacent relationship

Guess you like

Origin www.cnblogs.com/tylk/p/12579229.html