Java Implementation Sunday algorithm sample sharing _java

Source: Internet
Author: User

The two most famous of the string matching lookup algorithms are the KMP algorithm (Knuth-morris-pratt) and the BM algorithm (Boyer-moore). Two algorithms have a linear lookup time in the worst case. But in practice, the KMP algorithm is not much faster than the simplest C library function strstr (), and BM algorithm is often 3-5 times faster than the KMP algorithm (not hands-on). But BM algorithm is not the fastest algorithm, here is a faster than the BM algorithm Sunday algorithm to find the algorithm.

The idea of Sunday algorithm is very similar to the idea of bad characters in BM algorithm. The difference is only that the Sunday algorithm after the match fails, is to take the target string in the current and pattern string corresponding to the part of the character after a bad character match. When a match is found, the current offset in the +pattern string length + 1 (assuming K-position) is judged to be present in the pattern string. If it exists, the position is aligned with the character in the pattern string, and the match is started from the beginning, and if not, the pattern string is moved backwards, aligned with the character at the k+1 of the parent string, and then matched. Repeat the above action until you find it, or the parent string is finished. Write a small example to implement the following algorithm.

In the code, two kinds of string matching algorithms are implemented, one is the Sunday way, one is the normal way to move one bit at a time, the efficiency of the comparison in the main function is the nanosecond level. The detailed steps of the algorithm and the corresponding annotation has been added to the code. On the BM algorithm, the next time empty, and then control analysis.

Copy Code code as follows:

Import Java.util.HashMap;
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.Map;

/**
* @author Scott
* @date December 28, 2013
* @description
*/
public class Sundysearch {
String text = null;
String pattern = null;
int currentpos = 0;

/**
* Matching substring the first character position list
*/
list<integer> matchedposlist = new linkedlist<integer> ();

/**
* Match Character Map, record change match string have which char and the last occurrence of each char displacement
*/
Map<character, integer> map = new Hashmap<character, integer> ();

Public Sundysearch (string text, string pattern) {
This.text = text;
This.pattern = pattern;
This.initmap ();
};

/**
* Sunday match, used to store the last occurrence of each character in pattern, left to right order
*/
private void Initmap () {
for (int i = 0; i < pattern.length (); i++) {
This.map.put (Pattern.charat (i), i);

}
}

   /**
     * Normal string recursion match, go ahead with match failure a
     */
    public list<integer> Normalmatch () {
       //Match failed, Keep going down.
        if (!matchfromspecialpos (Currentpos)) {
             Currentpos + + 1;

if ((Text.length ()-Currentpos) < Pattern.length ()) {
return matchedposlist;
}
Normalmatch ();
} else {
Match succeeded, record position
Matchedposlist.add (Currentpos);
Currentpos + 1;
Normalmatch ();
}

return matchedposlist;
}

/**
* Sunday match, assuming the position of the K character in text is: Current offset +pattern string length +1
*/
Public list<integer> Sundaymatch () {
If no match is successful
if (!matchfromspecialpos (Currentpos)) {
If the K character in text does not appear in the pattern string, the length of the entire pattern string is skipped
if ((Currentpos + pattern.length () + 1) < Text.length ()
&&!map.containskey (Text.charat (Currentpos + pattern.length () + 1)) {
Currentpos + + pattern.length ();
}else {
If the K character in text appears in the pattern string, the position of the K character in text and the position of the last occurrence of the K character in the pattern string are aligned
if ((Currentpos + pattern.length () + 1) > Text.length ()) {
Currentpos + 1;
} else {
Currentpos + = Pattern.length ()-(Integer) Map.get (Text.charat (Currentpos + pattern.length ()));
}
}

Match completed, returns the initial displacement of all matching successes
if ((Text.length ()-Currentpos) < Pattern.length ()) {
return matchedposlist;
}

Sundaymatch ();
}else {
Match successfully forward one and then match again
Matchedposlist.add (Currentpos);
Currentpos + 1;
Sundaymatch ();
}
return matchedposlist;
}

/**
* Check whether the substring starting at the specified offset from text matches pattern
*/
public boolean matchfromspecialpos (int pos) {
if ((Text.length ()-pos) < Pattern.length ()) {
return false;
}

for (int i = 0; i < pattern.length (); i++) {
if (Text.charat (pos + i) = = Pattern.charat (i)) {
if (i = = (Pattern.length ()-1)) {
return true;
}
Continue
} else {
Break
}
}

return false;
}

public static void Main (string[] args) {
Sundysearch sundysearch = new Sundysearch ("Hello ah Adolph Adfsadfklf ADF234MASDFSDFDSFDSFDSFFWERWREWRERWERWERSDF2666SDFLSDFK "," ADF ");

Long begin = System.nanotime ();
System.out.println ("Normalmatch:" + sundysearch.normalmatch ());
System.out.println ("Normalmatch:" + (System.nanotime ()-begin));

Begin = System.nanotime ();
System.out.println ("Sundaymatch:" + sundysearch.sundaymatch ());
System.out.println ("Sundaymatch:" + (System.nanotime ()-begin));

}
}

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.