Data Structure-String Pattern Matching

Source: Internet
Author: User

what is pattern matching? In fact, it is to check whether there is a substring t in string S. Then T is called a matching string.
our usual search method is matching one by one, so the time complexity is O (n * m ).
for example, s = 'abcabde', t = 'abd'
S [1] = T [1], so next we will compare s [2] and T [2], and the result s [2] = T [2]. Then we will compare s [3] and T [3], this comparison does not match.
then, it is time-consuming for the common matching method: Compare s [2] and T [1] to continue the process above. In this way, we can see that the worst case needs to be compared to O (N * m) times.
draw a matching process:
1 abcabde
Abd match failed
2 abcabde
A match failed
3 abcabde
A match failed
4 abcabde
The Abd match is successful.
we can see four comparisons. So how can we use the Program to implement it?

Function commonindex (const sub, DEST: string): integer; var I, j: integer; len1, len2: integer; begin len1: = length (sub); len2: = length (DEST); Result: = 0; If len1> len2 then exit; J: = 1; I: = 1; // judgment process while (I <= len2-len1 + 1) AND (j <= len1) do if Dest [J] = sub [I] Then begin Inc (I ); INC (j); End else begin J: = 1; // sub backtracking I: = I-J + 2; // DEST backtracking end; // determine whether the search is successful if J = len1 then result: = I-J + 1; end;

Next let's take a look at how C is implemented:

Int commonindex (char * sub, char * DEST) {int I = 0, j = 0; while (Dest [I] & sub [J]) {If (Dest [I] = sub [J]) {++ I; ++ J;} else {J = 0; I-= J-1 ;}} // endwhile if (! Sub [J]) return I-j + 1; else return 0 ;}

As mentioned above, if the matching fails, both strings must be traced back, so it is necessary to waste time here.

WholeAlgorithmThe time complexity is O (M * n)

Let's take a look at the above matching process.

1 abcabde

Abd matching failed

In matching C and D, we find that we do not need to compare B, A, and C and A, because they certainly cannot match. So now we only need to backtrack the sub string.

Let's take a look at the improved matching process:

1 abcabde

Abd matching failed

2 abcabde

Abd matched

But how much does a sub trace after each matching failure? This is the essence of this KMP algorithm.

Let's take a look.CodeRight!

Function kmpindex (const sub, DEST: string): integer; var I, j: integer; len1, len2: integer; next: array of integer; Procedure makenext; // generate the next table begin I: = 1; J: = 0; next [1]: = 0; while I <= len1 do if (j = 0) or (sub [I] = sub [J]) then begin Inc (I); Inc (j); next [I]: = J; end else J: = next [J]; end; begin len1: = length (sub); len2: = length (DEST); Result: = 0; If len1> len2 then exit; setlength (next, len1 + 1); // The Dynamic Array starts from 0. So here + 1 is used to add one more element // generate a sub backtracking table. Makenext; try I: = 1; J: = 1; while (I <= len1) and (j <= len2) do if (I = 0) or (sub [I] = Dest [J]) Then // matched begin Inc (I); Inc (j); End else I: = next [I]; // failed. Only sub traces back if I> len1 then result: = J-I + 1; finally setlength (next, 0); end;

So what will C code do?

 int kmpindex (char * sub, char * DEST) {int Len = 0; // calculate the sub length for (; sub [Len]; ++ Len); // dynamically allocate the next array int * Next = (int *) malloc (LEN * sizeof (INT); // generate the next array int I = 0, K =-1; // * (next ++) = 0; next [0] =-1; while (I 
  

Let's look at the time complexity. Theoretically, it should be greater than O (N + M), less than O (N * m), and approximate O (N + M) in the actual process ).

From the code above, we can see that the I-th element of next is obtained by comparing the I-1-th element.

 
While (I <Len) if (k =-1 | sub [I] = sub [k]) {++ I; ++ K; // * (next ++) = K; next [I] = K;} else {k = next [k];}
 
While I <= len1 do if (j = 0) or (sub [I] = sub [J]) then begin Inc (I); Inc (j ); next [I]: = J; end else J: = next [J];

If the character starts from 1, next [0] = 0. If the character starts from 0, next [0] =-1;

Then we can manually calculate the next array word of 'abababd', assuming that the string starts from 1.

A B d

0 // the first one is directly equal to 0

0 1 // The second one depends on the first one. The first character is a, corresponding to the next [1] value is 0, then the comparison ends. Next [2] = 1

0 1 1 // The third is determined by the second. The second character is B, the corresponding K value is 1, and the first character is a, because B is not equal to a, continue to take the K value corresponding to the first character, K is 0, and the comparison ends. Next [3] = 1;

0 1 1 2 // The fourth depends on the third. The third character is a, the corresponding K value is 1, and the first character is a, because a = A, the comparison ends next [4] = 1 + 1 = 2

0 1 1 2 3 // The Fifth depends on the fourth. The fourth character is B, corresponding to the K value is 2, and the second character is B, because B = B, the comparison ends next [5] = 2 + 1 = 3

0 1 1 2 3 4 // similarly. The fifth character is a, the corresponding K value is 3, and the third character is a. Since a = A, the comparison ends, all next [6] = 3 + 1 = 4;

0 1 1 2 3 4 5 // The sixth character is B, the corresponding K is 4, and the fourth character is B. Because B = B, the comparison ends, so next [7] = 4 + 1 = 5;

The entire next array is 0 1 1 2 3 4 5

I don't know if you have understood the above process.

Of course, the next generation process is incorrect in special cases. So there is an improvement, nextval. Let's talk about this next time. Conditional friends can read books by themselves.

The book has its own gold house, and the book has its own face as Jade.

Both money and women come from books... Haha, it's no wonder that ancient scholars like to visit qinglou... It hurts .!!

Wait for my nextval. Thank you.

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.