Data structure and algorithm in JavaScript (iv): string (BF) _javascript tips

Source: Internet
Author: User
Tags radar

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.

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///Cycle number
 //BBC ABB abcf =>abc => Search 9 for
 (var i = 0; I <= Padding i++) {
  ///If the first charAt is met/
  /start sub cycle detection//
  /Where the SOURCESTR value is a value that needs to be superimposed 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)

function Bf_optimize (SOURCESTR, searchstr) {
  var mainlength  = sourcestr.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) 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.)

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 unequal corner sign back start match 
     i = i-j + 1;//I Fall back to the next j = 0 of the last match first 
     ;//J to fall back to the first of the 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

<!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/
  Generic version/////////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 SOURCESTR value is required to superimpose the value of I if (Sourcestr.charat (i) = = Searchstr.charat (0)) {//Match the successful data var complete = Searc
    Hlength;
      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 = Sourcestr
   . 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) return offset;
 } return-1;

   }//////////optimizer////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 letter unequal corner sign back start match i = i-j + 1;//I Fall back to the next j = 0 of the last match first;//J Back to substring first} if (j = =
     Earchstr.length) {return i-j; }}///////////brute force algorithm//recursive version/////////function bf_recursive (sourcestr, searchstr, offset) {var mainlengt
   h = 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_r
 Ecursive (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 does N ' 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 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 the 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 ' 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 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 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 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 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 20He told ABC News ' Diane Sawyer the 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 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 meeting on Thursday, the CEO of 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 mean spirited W Ay 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 () {R Eturn bf_optimize_1 (Sourcestr, Searchstr)}) Show (' Bf_optimize_2 ', function () {return bf_optimize_2 (SOURCESTR, search
 STR)}) Show (' Bf_recursive ', function () {return bf_recursive (Sourcestr, Searchstr)}) </script>

BF is also a classic prefix matching algorithm, 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 the KMP and BM algorithm for the BF upgrade

git code Download: https://github.com/JsAaron/data_structure

Related Article

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.