The forward-suffix expression of the string, and the KMP algorithm. __ algorithm

Source: Internet
Author: User
Tags stack pop

This paper mainly deals with the KMP suffix expressions of strings, and the algorithm of the first.

First of all, as we know, in C + +, a string has three forms of expression. Prefixes, infix, and suffix forms.

The difference is that the prefix expression consists of the operand immediately following the two operands. such as: *3 5

The operand of an infix expression is between two operands. such as: 3*5

The operand of the suffix expression follows the operand and is at the end. such as: 3 5*

Let's talk about the conversion of these three forms of expression.

The individual thinks infix expression converts to suffix expression correct conversion method: Iterate through the infix expression of each node, if: 1, the node is operand: direct copy into the suffix expression 2, the node is an operator, divided into the following situations: A, for "(operator: pressing in the temporary stack B, as") "Operator: constantly The operator that pops up the top operator of the temporary stack until the top is "("). And adds the operators that are ejected to the suffix expression C, for other operators, there are steps to compare the precedence of the operator with the top pointer on the temporary stack, if the top pointer of the temporary stack is greater than the precedence of the operator, pops up and adds to the suffix expression, and repeats the previous comparison work. Until a top of the stack pointer is encountered with a priority lower than the operator's priority, stop the pop-up add and press the operator onto the stack. The comparison process at this point, if the pointer at the top of the stack appears to be ' (', stops the loop and presses the operator onto the stack, note: ' Don't bounce. After traversing the infix expression, check the temporary stack, and if there are operators, all pop up and add to the suffix expression.
The individual thinks infix expression converts to prefix suffix expression correct conversion method: Traversal of infix expression of each node, if: 1, the node is for operands: press into the stack. 2, the node is an operator, divided into the following situations: A, to "(" Operator: the contents of the temporary stack pop-up, and put in a prefix expression.) B, is the "") Operator: no operation C, for other operators, directly into the prefix expression.
Here I give an infix expression: a+b*c-(d+e) First step: brackets all operands according to the precedence of the Operator: ((A + (b*c)-(d+e)) Step two: convert prefix to suffix expression prefix: Move the operation symbol to the corresponding bracket before it becomes:- (+ (A * (BC)) + (DE)) Remove the brackets: The-+a*bc+de prefix expression appears as a suffix: moving the operation symbol behind the corresponding bracket becomes: ((A (BC) *) + (DE) +)-Remove the parentheses: The abc*+de+-suffix shows no, prefix, suffix Type is not required to use parentheses to prioritize the determination. As Expressions: 3+ (2-5) *6/3
But the two methods form the form of the suffix expression is different: Infix expression: a+b* (c-d)/f-e formed by the suffix expression: 1. A b c d-f/* e-+
2. A b c D-* f/+ e-NOTE: The calculation of the prefix expression is not calculated according to the precedence of the calculation character, and is computed directly from left to right.
KMP Matching algorithm source code.

Problem Description: The prefix problem of a string. KMP algorithm, next array next array is used to store the string before the current character, and how long the same prefix suffix as next[4]=3 means is 3 of the same number of characters before and after the current character.                  For example: String AAAABBCDE. Its next number is:-1,-1, 1,-1, 3, 0, 0, 0, 0 so KMP's matching algorithm core differs from the brute force matching method in that if the current J-character match fails, it does not start with a new match, Instead, it extracts the next character of the longest prefix that precedes the current character stored in the next array and starts again. This method improves the overall performance. The modified function code for the values of the next arrayvoidGet_nextval (CharConst* PTRN,intPlenint* Nextval) {inti = 0; Nextval[i] =-1;intj =-1; while(I < plen-1) {if(j = = 1 | | ptrn[i] = = Ptrn[j])//The If part of the loop {++i;                 ++j; Where the correction takes place, the following 4 linesif(Ptrn[i]!= ptrn[j])//++i,++j, again to judge Ptrn[i] and Ptrn[j's relationship nextval[i] = j; The previous error solution lies in the whole judgment only this sentence.ElseNextval[i] = Nextval[j]; }ElseThe else part of the loop J = Nextval[j]; }     }

KMP//int Kmp_seach (char const*, int, char const*, int, int const*, int pos) KMP pattern matching function/input: SRC, slen main string/input: Patn , Plen mode string//input: An array of next function values in the Nextval KMP algorithmintKmp_search (CharConst* SRC,intSlen,CharConst* Patn,intPlenintConst* Nextval,intPOS) {inti = pos;intj = 0; while(I < Slen && J < Plen) {if(j = = 1 | | src[i] = = Patn[j])                 {++i;             ++j; }Else{j = nextval[j]; When the match fails directly with P[j_next] and s[i],//The following describes how to find this value, that is, the next match after the failure of matching the position}}if(J >= Plen) returnI-plen;Else return-1; }

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.