This article mainly introduces JavaScript data structure and algorithm (four): string (BF), the string is composed of 0 or more characters of the finite sequence, also known as strings, this article focuses on the BF (brute Force) algorithm, the need for friends can refer to the
 
A string is a finite sequence of 0 or more characters, also known as a string.
 
The logical structure of a string is similar to that of a linear table, and the difference is that a string is a character set, so there is a big difference between the operation and the linear table. The linear table is more concerned with the operation curd of individual elements, while the string is concerned with finding the position of substring, substitution and so on.
 
Of course, different high-level languages have different methods of defining the basic operation of a string, but the essence of the operation is similar in general. For example Javascrript search is indexof, go blank is trim, transform the case tolowercase/touppercase and so on
 
Here we mainly discuss several classical algorithms for string pattern matching: BF, BM, KMP
 
BF (Brute Force) algorithm
 
The basic idea of Brute-force algorithm:
 
Compares the first character of the target string s with the Fu Qi of the pattern string T, and if it is equal, continues to compare the subsequent characters individually, otherwise the second word from the string s is Fu Qi and the string T is compared again.
 
And so on, until each character in the string T is equal to a contiguous sequence of characters in the string s, the pattern match succeeds, and the position of the first character of the string T in the string s is the position of T in S, otherwise the pattern match is unsuccessful
     
It can be seen that the BF algorithm is a brute force algorithm, also known as a simple matching algorithm or brute forces algorithm.
 
Main string BBC ABB ABCF
 
Sub-string ABC
 
Find the position of the substring in the main string, and correspond to the realization of the IndexOf lookup method which is actually JavaScript.
 
?
 
 
  
   
   | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |  
   var sourcestr = "BBC ABB abcf";   var searchstr = "ABC"; function Bf_ordinary (SOURCESTR, searchstr) {var sourcelength = sourcestr.length; var searchlength = Searchstr.length; var padding = sourcelength-searchlength; Number of cycles//BBC ABB abcf =>abc => Search 9 for (var i = 0; I <= padding; i++) {//If the first charat is equal//start sub loop detection//Where sour The value of the CESTR is the value of the overlay I if (sourcestr.charat (i) = = Searchstr.charat (0)) {//Match the successful data var complete = searchlength; for (var j = 0; J < Searchlength; J + +) {if (Sourcestr.charat (i + j) = = Searchstr.charat (j)) {--complete if (!complete) {return i;}}} } return-1; } |  
  
 
  
 
The BF algorithm is simply rough, directly to the BBC ABB ABCF the list of each character of the following table out to match the first character of the pattern string, if equal to enter the string of the second match
 
It is worth noting here:
 
1: The most peripheral loop number sourcelength-searchlength, because we match the parent string at least greater than or equal to the substring
 
2: In the continued match of the substring, the starting point of the parent string is to be superimposed (I+J)
 
3: By a condition to determine whether the exact match COMPLETE,BBC ABB ABCF, we need to jump over ABB
 
Above is the simplest algorithm, the code also has a better processing, such as in the string matching can take the inverse algorithm
 
Optimization algorithm (i)
 
?
 
 
  
   
   | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |  
   function Bf_optimize (SOURCESTR, searchstr) {var mainlength = sourcestr.length; var searchlength = searchstr.length; var p adding = mainlength-searchlength; for (var offset = 0; offset <= padding. offset++) {var match = true; for (var i = 0; i < searchlength; i++) {//reverse, If as long as not equal if (Searchstr.charat (i)!== Sourcestr.charat (offset + i)) {match = false; break;}} if (match) return offset; } return-1; } |  
  
 
  
 
We do not need to judge the situation as true, we only have to judge the situation is false, when the match has not been modified after the match, then the matching is the exact match
 
All of the above 2 methods we have used the sub cycle, can we change into a loop body?
 
In fact, we can see the rule, the main string will only increase by +1 each time, the substring of each match is also the beginning of the match, so we can change to a while, control the subscript pointer can be
 
Optimization Algorithm (II.)
 
?
 
 
  
   
   | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 |  
   function bf_optimize_2 (SOURCESTR, searchstr) {var i = 0, j = 0; while (I < sourcestr.length) {//two letter equal continues if (Sourcestr.charat (i) = = Searchstr.charat (j)) {i++; j + +} else {//two letter not Wait for the corner sign back again to start the match i = i-j + 1; I fall back to the last match first of the next j = 0;   J back to the first of substring} if (j = = Searchstr.length) {return i-j;} } } |  
  
 
  
 
I is the subscript position of the main string, J is the subscript positioning of the substring
 
When the main string substring is equal, it enters the loop mode of the substring, and when the number of sub loops satisfies the substring length, the verification is exactly matched
 
When the main string substring is not equal, it is necessary to move the subscript of the main string backward one, of course I, because it may be processed by the substring, so need to i-j+1, and then duplicate the seat string
 
Specifically we can look at code comparisons
 
Four kinds of structures based on BF algorithm, for/while/recursion
 
?
 
 
  
   
   |  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 112 113 114 116 117 118 119 121 122 123 124-126 127 128 129 130 131 132 |  
    <!doctype html> due to the continuous improvement of computer performance, the size of the measured data may result in inaccurate results; <script type= "Text/javascript" >  /////////// Brute force algorithm///General edition/////////function bf_ordinary (sourcestr, searchstr) {var sourcelength = sourcestr.length; var searchlength = Searchstr.length; var padding = sourcelength-searchlength; Number of cycles  //BBC ABB abcf =>abc => Search 9 for (var i = 0; I <= padding; i++) {//If the first charat is equal//start sub cycle detection/ /Where the value of SOURCESTR is a value that requires superposition I if (sourcestr.charat (i) = = Searchstr.charat (0)) {//Match the successful data var complete = searchlength; for (var j = 0; J < Searchlength; J + +) {if (Sourcestr.charat (i + j) = = Searchstr.charat (j)) {--complete if (!complete) {return i;}}} } return-1; }    ///////////brute force algorithm////////////function Bf_optimize_1 (sourcestr, searchstr) {var mainlength = source Str.length; var searchlength = searchstr.length; var padding = mainlength-searchlength; for (var offset = 0; offset <= padding. offset++) {var match = true; for (var i = 0; i; Searchlength; i++) {//reverse if as long as not equal if (Searchstr.charat (i)!== Sourcestr.charat (offset + i)) {match = false; break;}} if (match) retur n Offset; } return-1; }    //////////optimized////while////////function bf_optimize_2 (sourcestr, searchstr) {var i = 0, j = 0;   while (I < sourcestr.length) {//two letter equal continues if (Sourcestr.charat (i) = = Searchstr.charat (j)) {i++; j + +} else {//two letters Unequal corner sign back to start again match i = i-j + 1; I fall back to the last match first of the next j = 0; J fallback to substring first}   if (j = = Searchstr.length) {return i-j;}  }}    ///////////brute force algorithm//recursive version///// function bf_recursive (sourcestr, searchstr, offset) {var mainlength = sourcestr.length; var searchlength = Searchstr . length; if (Searchlength > Mainlength-offset) {return-1} offset = Offset | | 0; for (var i = 0; searchlength > I. i++) {if (Searchstr.charat (i)!== Sourcestr.charat (offset + i)) {return Bf_recursiv E (sourcestr, searchstr, offset + 1)} return offset; }       var souRcestr = "There are some times Wthere are some times when clicking" like "on a friend ' s Facebook status doesn ' t feel appro Priate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:faceboOK founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural that a "dislike" button could solve THe conundrum of the wanting to empathize but not seem inappropriate by clicking ' like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere are some TimES when clicking "like" on a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that's Facebook would "definitely thinkhen clicking" like "on a friend ' s Facebo Ok status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The ideahas been on Mark Zuckerberg's radar for "a while," he said. In the he told ABC News ' Diane Sawyer There are some times when clicking "like" on a friend ' s Facebook status doesn ' t fe El appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Restof Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A break up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that ' Facebook would ' definitely thinkthere are the times when some "On a friend ' s Facebook status doesn ' t feel appropriate. A Bad Day. A loved one lost. A BreAK up. It only seems natural this a "dislike" button could solve the conundrum of wanting to empathize but not seem By clicking ' Like. ' Mark Zuckerberg Puts the Rest of Us to shame by Speaking Fluent Chinese. Mark Zuckerberg:facebook founder and Animal Butcher. Mark Zuckerberg and that shirt. The idea has been on Mark Zuckerberg's radar for a while and he said. In the he told the ABC News ' Diane Sawyer that's that Facebook would ' definitely ' so that's Facebook would ' definitely th Ink about "adding a dislike button. "People definitely seem to want it," Zuckerberg said. Four years Later-zuckerberg says Facebook is still "thinking about" adding the oft-requested sdfafd button, Zuckerberg s Ays Facebook is still "thinking about" adding the oft-requested button. At a town hall of Thursday, the CEO revealed he has some reservations about the feature. "There are two things that it can mean," Zuckerberg said of the potential button, which could is used in a meanWay or to express empathy. Finding to limit it to the latter is the challenge. Zuckerberg said he doesn ' t want the button to turn into a "voting mechanism" or something that isn ' t "socially valuable." "Often people'll tell us they don ' t feel comfortable pressing ' like, '" Zuckerberg said. "What ' s right way to make it so people can easier express a wide range of emotions?" One suggestion percolating Online:aaron Roll out the feature under a different name. However, a "empathy button" just may not have the same ring to it as "dislike"; var searchstr = "Adding the oft-requested SDF";       function Show (BF_NAME,FN) {var mydate = +new Date () var r = fn (); var div = document.createelement (' div ') div.innerhtml = Bf_name + ' algorithm, search location: ' + R + ', time consuming ' + (+new Date ()-mydate) + "MS"; Document.body.appendChild (DIV);   Show (' Bf_ordinary ', function () {return bf_ordinary (Sourcestr, Searchstr)})   Show (' Bf_optimize_1 ', function () {return bf_optimize_1 (SOURCESTR,SEARCHSTR)   Show (' bf_optimize_2 ', function () {return bf_optimize_2 (Sourcestr, Searchstr)})   Show (' Bf_ Recursive ', function () {return bf_recursive (Sourcestr, Searchstr)})   </script> |  
  
 
  
 
The 
 Bf is also a classic prefix matching algorithm, the prefix also includes KMP, we can see that the biggest disadvantage of this algorithm is the character matching failure pointer will backtrack, so the performance is very low, then write KMP and BM algorithm for the BF upgrade