Basic string matching algorithm

Copyright: Original article reproduced please indicate the source. https://blog.csdn.net/samll_snail/article/details/90438219

 

Tan has recently been asked a question, programming language string matching function is how to achieve, what is the principle.

 

It seems the big cat to eat again show his face against the talented big move.

 

Xiao Tan while my heart whispered, so what can this principle, not directly on the line, Guannameduo doing, and said unto big cats, and today they want to eat what the surface.

 

The big cat is so often ask you a question, you will not, then let you ask him to eat a bowl of noodles, he tell you clearly.

 

Big Cat shy smile said today eat a bowl of noodles on the line, it is estimated that today's technical content is not high enough big move it, give a bowl of noodles to deal with.

 

Sure enough, the big cat just five words to describe his big move, violence matching , Tan an Violence Act, frowned, seemingly also think of the answer, big cats and quickly went on to add.

 

Violence matching is to use the most primitive and simplest of methods to match the two strings are referred to as target string and pattern string, then to be determined whether the target string to match the pattern string.

 

Small Tanling Guang flash, can not wait to tell their own ideas, this simple ah, not that start comparing the first character of the target string and the first character string mode, if two characters are the same, continue to compare the two behind the character, otherwise, the pattern will move to the target string of the second character string start comparing again .

 

 

Big cat a little anxious, seeing his big move already be seen through, and quickly summed up some homeopathy.

 

Is da, this is the most conventional way of thinking, is the most simple algorithms, this violence matching algorithm is called  BF (bure force)  algorithm.

 

Tan asked to remain, the algorithm lacks technical content, stupid method is used, there is no better a little, or not worth the bowl of ramen.

 

Yes, there is really little better than this, but to add a spiced corned can. It looks big cat already prepared, the full routine.

 

We can use a hashing algorithm to optimize the BF algorithm, big cat looks very proud of the way.

 

Why use a hash of it, how to use, Xiao Tan not quite understand.

 

You see in BF algorithm, each time to take the target string and a string comparison of a character pattern of character, this is very time-consuming.

 

If the target string length is a, the length of the string pattern is b, then in fact the target string can be split into (a-b + 1) ... substrings

 

Etc., why is (a-b + 1) of it, Tan bit puzzled.

 

I'll draw a map you will understand, the big cat on the phone said as he began to draw from the map.

 

 

6 target string length, string pattern length is 3, a total of four target string substrings

 

Next, this fact (a-b + 1) th string and compares each pattern string can.

 

That there is no particular ah, useless to the hash function, ah, Xiao Tan followed ask the big cats.

 

Wait ah, the key in the hash function below, we pattern string and each substring of the target string uses a hash function to obtain a hash value, then we can directly compare their corresponding hash value on it, because the hash value is a number, a comparison between the figures is very fast . Accordingly, it is determined whether the pattern string in the target string, if there is only need to compare the two hash values are equal like.

 

This clever idea a little, ah, Xiao Tan feeling a little enlightened.

 

This is actually just a thought, the core is the design of the hash function, such as how to deal with if there is a hash conflict, how to traverse only once the target string can calculate the hash value substring of all, these are the hash function to solve the problem, and this algorithm is called  RK  algorithm, it is the first letter of the name of a combination of two foreigners.

 

Bowl of ramen finally worth it, but in the end this particular design of the hash function next time have to continue to make it clear ah. . .

 

No problem ah, but I have to go back and look up information to re-learn, the fight next time you talk to understand, to tell you today is the main idea. . .

Guess you like

Origin blog.csdn.net/samll_snail/article/details/90438219