string and string pattern matching algorithm (substring lookup) BF algorithm, KMP algorithm

Source: Internet
Author: User

Fixed-length sequential storage of strings
#define Maxstrlen 255,//exceeds this length then the part is abandoned, called truncation

Pattern Matching for strings:


Definition of a string: a finite sequence consisting of 0 or more characters
S = ' a1a2a3.......an '
n = 0 O'Clock is an empty string
The sequential storage structure of the string: the character array, the length of the string is the number of characters before the end of the array
Array must be defined at length, with limitations
Maximum length of an array
Two: Heap allocation storage representation of strings
typedef struct {
Char *ch;
If the string is not empty, the storage area is allocated by string length
Otherwise ch is empty
int length; String length
}hstring;
The system uses functions Mallloc and free to dynamically allocate the string value space, resulting in a new string
In fact, the system first allocates a storage space for the newly generated string and then replicates the string (this is the string type of C
Storage mode)

Three, string of block chain storage mode

1 #defineCHUNKSIZE 802typedefstructchunk{//node Structure3 CharCh[chunksize];4 structchunk*Next;5 }chunk;6 7typedefstruct{//linked list structure of a string8Chunk*head, *tail;//head and tail pointers for strings9 intCurlen;//the current length of the stringTen}lstring;

Data | Pointer
1byte 4byte
1/5 Storage Density

4.3-String pattern matching algorithm (substring lookup)
BF algorithm: Naïve algorithm

1 intIndex (sstring S, sstring T,intPOS)2 {3i = pos; j =1;4  while(I <= s[0] && J <= t[0])5 {6 if(S[i] = =T[j])7 {8++i;9++J;Ten } One Else     A { -i = I-j +2;//I pointer backtracking -j =1;//The pointer backs back to start matching the } - if(J > t[0]) - returni-t[0]; - Else + return 0; - } +}

1 intIndex (sstring S, sstring T,intPOS)2 {3  for(i = pos; I <= s[0]-t[0]; i++)4 {5 intK =i;6  for(j =1; J <= t[0]; J + +)7 {8 if(S[i] = =T[j])9 {Teni++; OneJ + +; A } - Else - { thei =K; -  Break; - } - } + if(J > t[0]) - returni-t[0]; + Else A return 0; at } -}

Two, the end-to-end matching algorithm
Compare the first character of a pattern string first
Compare the last character of the pattern string again
Finally compare the second one in the comparison pattern string to the second one to get the last character between the penultimate
Algorithm complexity is the same as the first type O ((n-m+1) m)

Three, KMP algorithm
Time complexity up to O (m+n)

1 intIndex (sstring S, sstring T,intPOS)2 {3i = pos; j =1;4  while(I <= s[0] && J <= t[0])5 {    6 //J = = 0 indicates that the first character on the previous comparison is not equal to next[1] = 07 if(J = =0|| S[i] = =T[j])8 {9++i;Ten++J; One } A Else     - { -j = Next[j];//I do not use pointer backtracking the //J pointer back to Next[j] start match again - } - } - if(J > t[0]) + returni-t[0]; - Else + return 0; A  at}

Find Next function value
Known: next[1] = 0;
Hypothesis: next[j] = k; And because t[k] = T[j]
Then next[j+ 1] = k + 1;
Ruo T[j]! = T[k]
You need to go back, check t[j] = t[?]
This is also a matching process, different: the main string and the pattern string are the same string

1 voidGet_next (sstring &t,int&next[])//find the next function value of the pattern string T and deposit the array next2 {3i =1; next[1] =0;4j =0;5  while(I < t[0])6 {7 if(J = =0|| T[i] =T[j])8 {9++i;Ten++J; OneNext[i] =J; A } -  - Else thej =Next[j]; - if( - } -  +}


Special cases
S = ' Aaabaaabaaabaaabaaab '
T = ' Aaaab '
NEXT[J] = 01234 corrected after 00004

1 voidGet_next (sstring &t,int&next[])//find the next function value of the pattern string T and deposit the array next2 {3i =1; next[1] =0;4j =0;5  while(I < t[0])6 {7 if(J = =0|| T[i] =T[j])8 {9++i;Ten++J; One if(T[i]! =T[j]) ANextval [i] =J; - Else -Nextval[i] =Next[j]; the } -  - Else -j =Nextval[j]; + if( - } +  A}

string and string pattern matching algorithm (substring lookup) BF algorithm, KMP algorithm

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.