Data structures and algorithms in JavaScript (4): string (BF ),

Source: Internet
Author: User
Tags radar

Data structures and algorithms in JavaScript (4): string (BF ),

A string is a finite sequence composed of zero or multiple characters. It is also called a string.

The logical structure of a string is similar to that of a linear table. The difference is that a string is a character set. Therefore, the operation is quite different from that of a linear table. A linear table is more concerned with the operation CURD of a single element, while a string is concerned with operations such as searching for the position and replacement of a substring.

Of course, different advanced languages have different definitions for the basic operations of strings, but in general, the operations are essentially similar. For example, if you use javascrript to search for objects, you can use indexOf, trim to empty objects, toLowerCase to convert to toUpperCase, and so on.

Here we mainly discuss several classic algorithms for String Matching: BF, BM, and KMP.

BF (Brute Force) Algorithm

The basic idea of the Brute-Force algorithm:

Compare the first character of the target string s with the first character of the mode string t. If they are equal, continue to compare subsequent characters one by one, otherwise, repeat the string t from the second character of string s.

So far, when each character in string t is the same as a continuous Character Sequence in string s, the pattern match is successful, at this time, the first character of string t in string s is the position of t in s. Otherwise, the pattern match fails.


It can be seen that BF is a brute force algorithm, also known as simple matching algorithm or brute force algorithm.

Main BBC ABB ABCF

Substring ABC

Find the position of the sub-string in the main string, which corresponds to the implementation of the javascript indexOf lookup method.

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 times for (var I = 0; I <= padding; I ++) {// if the first charAt is equal, // start the subloop detection // where the value of sourceStr is the value of I that needs to be superimposed if (sourceStr. charAt (I) = searchStr. charAt (0) {// match successful 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 simple and crude. The following table of each character in the bbc abb abcf mother string matches the first character in the pattern string.

Note:

1: sourceLength-searchLength, because the matched mother string must be at least equal to or greater than the child string.

2: In the continued matching of substrings, the starting point of the parent string must be superimposed (I + j)

3: A condition is used to determine whether the complete is fully matched. In the bbc abb abcf, we need to jump over when we are at ABB.

The above is the simplest algorithm, and there is better processing in the Code. For example, an inverse algorithm can be used for self-string matching.

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 ++) {// returns the inverse if (searchStr. charAt (I )! = SourceStr. charAt (offset + I) {match = false; break ;}} if (match) return offset ;}return-1 ;}

We do not need to judge the case as true. We only need to judge the case as false. If the match is not modified after the child match ends, it indicates that the match is a full match.

The above two methods all use subloops. Can we change them to a loop body?

In fact, we can see that the primary string will only increase by 1 each time, and the Child string will be matched from the beginning each time. So we can change it to a while to control the subscript pointer.

Optimization Algorithm (2)

Function BF_Optimize_2 (sourceStr, searchStr) {var I = 0, j = 0; while (I <sourceStr. length) {// if (sourceStr. charAt (I) = searchStr. charAt (j) {I ++; j ++;} else {// if the two letters are not the same, the badge is backward and the matching I = I-j + 1 is re-started; // I rolled back to the next j = 0 in the first position of the last match; // j rolled back to the first position in the substring} if (j = searchStr. length) {return I-j ;}}}

I is the subscript location of the Main string, and j is the subscript location of the Child string.

When the primary string and the Child string are equal, the loop mode of the Child string is entered. When the number of times j of the sub-loop satisfies the length of the Child string, the full match is verified.

When the sub-strings of the Main string are not equal, you need to move the sub-scripts of the Main string one by one. Of course, when I is, it may be processed by the sub-string, therefore, I-j + 1 is required, and then the substring is reset.

For more information, see the code comparison.

Four Structures Based on BF algorithm, for/while/Recursion

<! Doctype html> the results may be inaccurate due to the continuous improvement of computer performance and the size of the tested data volume; <script type = "text/javascript"> //////// brute-force algorithm // normal 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 times for (var I = 0; I <= padding; I ++) {// if the first charAt is equal, // start the subloop detection // The value of sourceStr Is the value if (sourceStr. charAt (I) = searchStr. charAt (0) {// 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 //// optimized version ///// // 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 ++) {// returns the inverse if (searchStr. charAt (I )! = SourceStr. charAt (offset + 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) {// if (sourceStr. charAt (I) = searchStr. charAt (j) {I ++; j ++;} else {// if the two letters are not the same, the badge is backward and the matching I = I-j + 1 is re-started; // I rolled back to the next j = 0 in the first position of the last match; // j rolled back to the first position of the substring} if (j = s EarchStr. 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_Recursive (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 appropriate. A bad day. A loved one lost. A break up. it only seems natural that a "dislike" button cocould solve the conundrum of wanting to empathize but not seem inap Propriate 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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "definitely thinkThere are some times when 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 that a "dislike" button cocould 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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "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 that a "dislike" button cocould solve the conundrum of wanting to empathize but not seem inappropriate by clickin G "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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "definitely thinkThere 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 that a "dislike" button cocould 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 ra Dar for a while, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "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 that a "dislike" button cocould solve the conundrum of wanting to empathize but not seem inappropriate by clicking "like. "Mark Zucke Rberg 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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "definitely thinkThere are some times when clicking" like "on a friend's Facebook status doesn' t feel appropriate. A ba D day. A loved one lost. A break up. it only seems natural that a "dislike" button cocould 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, he Said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "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 cocould solve the conundrum of wanting to empathize but not seem inappropriate by clicking "like. "Mark Zuckerberg Puts the Rest of Us to Shame by Spea King 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, he said. in 2010, he told ABC News 'diane Sawyer There 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 that a "dislike" button c Ocould 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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "definitely t HinkThere 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 that a "dislike" button cocould 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 Ani Mal Butcher. mark Zuckerberg and That Shirt. the idea has been on Mark Zuckerberg's radar for a while, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "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 that a "dislike" button cocould solve the conun Drum 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, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wocould "definitely thinkThere are some t Imes 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 cocould 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 Zu Ckerberg and That Shirt. the idea has been on Mark Zuckerberg's radar for a while, he said. in 2010, he told ABC News 'diane Sawyer that Facebook wowould "definitely think that Facebook wowould" definitely think 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 s Dfafd button, Zuckerberg says Facebook is still "thinking about" adding the oft-requested button. at a town hall meeting on 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 cocould be used in a mean spirited way or to express empathy. finding how to limit it to the latter is the challenge. Z Uckerberg said he doesn't want the button to turn into a "voting mechanic" or something that isn't "socially valuable. "Often people will tell us they don't feel comfortable pressing 'like, '" Zuckerberg said. "What's the 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, an "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 consumed" + (+ 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>

BF is also a classic prefix matching algorithm, and the prefix also includes KMP. We can see that the biggest drawback of this algorithm is that if the character matching fails, the pointer will be traced back, so the performance is very low, then I will write about the BF upgrade for the KMP and BM algorithms.

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.