Data Structures and Algorithms in JavaScript (4): string (BF) _ javascript skills

Source: Internet
Author: User
Tags radar
This article mainly introduces the data structure and algorithm in JavaScript (4): string (BF). a string is a finite sequence consisting of zero or multiple characters. it is also called a string, this article focuses on the BF (BruteForce) algorithm. For more information, see the finite sequence of strings consisting 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

The computer performance is constantly improved, and the size of the tested data volume may lead to inaccurate results;

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.