Data structures and Algorithms JavaScript (four) strings (BF)

Source: Internet
Author: User
Tags string back radar

String is a finite sequence of 0 or more characters, also called a string

The logical structure of a string is very similar to a linear table, and the difference is that a string is a character set, so it is very different from a linear table in operation. The linear table is more concerned with the operation of a single element curd, the string is concerned with finding the location of the substring, substitution and other operations.

Of course, different high-level languages have different definitions for the basic operations of strings, but generally the nature of operations is similar. For example Javascrript search is indexof, go blank is trim, convert 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:

From the first character of the target string s character and the first characters of the pattern string T are compared, if equal, continue to compare the subsequent characters one by one, or from the second character of the string s character and then again and string T comparison.

And so on, until each character in the string T is equal to a continuous sequence of characters of the string s, the pattern match succeeds, and the first character of the string T in the string s position is the position of T in S, otherwise the pattern match is unsuccessful

Visible BF algorithm is a kind of brute force algorithm, also known as naïve matching algorithm or brute strength algorithm.

Main string BBC ABB ABCF

Sub-string ABC

Finding the location of the substring in the main string corresponds to the implementation of the IndexOf lookup method that is actually JavaScript.

var " BBC ABB ABCF " ; var " ABC ";
function Bf_ordinary (sourcestr, searchstr) {varSourcelength =sourcestr.length; varSearchlength =searchstr.length; varpadding = sourcelength-searchlength;//Number of Cycles//BBC ABB abcf =>abc = 9 times found   for(vari =0; I <= padding; i++) {    //If the first Charat is met, it is equal.//start sub-cycle detection//where the value of SOURCESTR is the value that needs to be superimposed I    if(Sourcestr.charat (i) = = Searchstr.charat (0)) {      //Match Successful data      varComplete =searchlength;  for(varj =0; J < Searchlength; J + +) {        if(Sourcestr.charat (i + j) = =Searchstr.charat (j)) {          -- Completeif(!Complete ) {            returni; }        }      }    }  }  return-1;}

The BF algorithm is simply rude, which directly takes the following table of every character of the BBC ABB ABCF string and matches the first character of the pattern string, and if it is equal, the string is matched again.

It is worth noting here:

1: The number of peripheral loops is sourcelength-searchlength, because we match a string at least greater than or equal to the substring

2: In the continuation match of the substring, the starting point of the parent string needs to be superimposed (I+J)

3: By a condition to determine whether the exact match COMPLETE,BBC ABB ABCF, we need to jump over when we're in ABB.

Above is the simplest algorithm, the code also has better processing, such as in the self-string matching can take the inverse of the algorithm

Optimization algorithm (i)

function Bf_optimize (sourcestr, searchstr) {varMainlength =sourcestr.length; varSearchlength =searchstr.length; varpadding = mainlength-searchlength;  for(varoffset =0; Offset <= padding; offset++) {      varMatch =true;  for(vari =0; i < searchlength; i++) {        //take the inverse, if not equal        if(Searchstr.charat (i)!== Sourcestr.charat (offset +i) {match=false;  Break; }      }      if(match)returnoffset; }    return-1;}

We do not need to judge the situation to be true, we just have to judge the situation is false, when the match has not been modified after the match, then it is an exact match

The above 2 methods we all use the sub-cycle, can we change into a loop body?

In fact, we can see the rule, the main string will only increment +1 each time, the substring matches each other from the beginning, so we can change to a while, control the subscript pointer can be

Optimization Algorithm (II.)

function bf_optimize_2 (sourcestr, searchstr) {vari =0, J=0;  while(I <sourcestr.length) {//Two letters are equal then continue        if(Sourcestr.charat (i) = =Searchstr.charat (j)) {i++; J++; } Else{//two letters unequal then the corner mark back to start the match againi = I-j +1;//I fall back to the next last match firstj =0;//J fallback to the first of the substring        }        if(J = =searchstr.length) {returnIJ; }    }}

I is the subscript positioning of the main string, and J is the subscript location of the substring.

When the main string string is equal, it enters the loop mode of the substring, and when the number of cycles J satisfies the substring length, the validation is exactly the same.

When the main string string is not equal, it is necessary to move the subscript of the main string back one, of course I, because it is possible to pass the substring processing, so need to i-j+1, and then re-seat string

Specifically we can look at the code comparison

<!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// Normal 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 times for (var i = 0; I <= padding; i++) {//If the first Charat is met// Start sub-loop detection//Where the value of SOURCESTR is the value that needs to be superimposed I if (sourcestr.charat (i) = = Searchstr.charat (0)) {//Match successful data var C Omplete = Searchlength; for (var j = 0; J < Searchlength; J + +) {if (Sourcestr.charat (i + j) = = Searchstr.charat (j)) {--co Mplete if (!complete) {return i; }}}}} return-1; }///////////brute force algorithm/////Optimized version/////////function bf_optimize_1 (sourcestr, searchstr) {var mainlength = Sourcest R.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++) {//inverse if not equal if (Searchstr.charat (i)!== Sourcestr.charat (of Fset + i) {match = false; Break }} if (match) return offset; } return-1; }//////////optimized version////while////////function bf_optimize_2 (sourcestr, searchstr) {var i = 0, j = 0; while (I < sourcestr.length) {//Two letters are equal then continue if (Sourcestr.charat (i) = = Searchstr.charat (j)) { i++; j + +; } else {///two letters unequal then corner mark back start match i = i-j + 1;//I Fall back to the next one in the first match j = 0;//J Fall back to the first of the substring} 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)) {RE Turn bf_recursive (sourcestr, SEARCHSTR, offset + 1)}} return offset; } var sourcestr = "There is some times wthere is 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 that a "dislike" button could solve the conundrum of wanting to empathize and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook WOuld "Definitely thinkthere is some times when clicking" as "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In $, he told ABC News ' Diane Sawyer that's Facebook would "definitely thinkhen 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 that a "dislike" button could solve the conundrum of wanting to empathize and 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 had been on Mark Zuckerberg's radar for a while, he said. In, he told ABC News ' Diane Sawyer there is 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 that a "dislike" button could solve the conundrum of wanting to empathize and 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 had been on Mark Zuckerberg's radar for a while, hesaid. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In the he told ABC News ' Diane Sawyer that's Facebook would definitely thinkthere is some times when clicking "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 and 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 had been on Mark Zuckerberg's radar for a while, he said. In, he told ABC News ' Diane Sawyer this that's Facebook would "definitely think 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 says Facebook is still "thinking" adding the oft-requested button. At a town hall meeting on Thursday, the CEO revealed he had some reservations about the feature. "There is 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 how 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 would tell us they don't feel comfortable pressing ' like, '" Zuckerberg said. "What's the right-of-the-people can easier express a wide range of emotions?" One suggestion percolating Online:aaron roll out the feature under a different name. However, an "empathy button" just could not be 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 the classic prefix matching algorithm, the prefix also includes KMP, we can see the biggest disadvantage of this algorithm is that the character matching failure pointer is going back, so the performance is very low, then will write the KMP and BM algorithm for the BF upgrade

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

Data structures and Algorithms JavaScript (four) strings (BF)

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.