高速字符串匹配算法

时间:2012-07-26 06:30:53

标签: string algorithm

我基本上对一些高速字符串匹配算法进行了基准测试,我遇到了一些。

  1. 向后非确定性DAWG(有向无环字图) Gonzalo Navarro和Mathieu Raffinot的匹配算法。见" A. 后缀自动机的位并行方法:快速扩展字符串 匹配"

  2. Horspool改进版的Boyer-Moore String 搜索算法。请参阅"实用快速搜索字符串"

  3. 具有不匹配的Shift-Or算法

  4. KMP

  5. 我可以尝试其他更好的高速字符串匹配算法吗?

    编辑:在类似的行中还有另一个thread,它也有很好的引用

3 个答案:

答案 0 :(得分:1)

您也可以尝试

答案 1 :(得分:1)

据我所知,双向字符串匹配是字符串匹配的最佳通用算法。它具有线性最坏情况复杂性,使用恒定空间,并且不会超出必要的回溯。其背后的理论非常好。

如果您知道您的用户不是混蛋,那么为您的架构优化的天真字符串匹配将赢得短“针”,而Boyer-Moore变体将开始真正为长“针”做次要线性的事情。然而,天真的字符串匹配具有二次最坏情况,并且可以使用Boyer-Moore来检查输入中的所有字符。处理不匹配所需的额外表格实际上对双向字符串匹配造成了惊人的严重损失。

答案 2 :(得分:-1)

import java.util.Scanner;

public class StringMatch {

static int temp,i=0,j=0; static boolean flag=true,matcher=false;

    static String str=null,mstr=null;static char astr[],amstr[];

        static void getter(){
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        //String str="today is Monday"; 
        astr=str.toCharArray();
         mstr = sc.nextLine();
        //String mstr="is"; 
         amstr=mstr.toCharArray();
    }
    static void stringMatch(){
        while(i<astr.length){
            if(astr[i]==amstr[j]){
            while((j!=amstr.length)&&flag){temp=i;
                if(astr[i]!=amstr[j]) {flag=false;matcher=false;}
                else{matcher=true;}
                i++;j++;
                //System.out.println(i+"\t"+j);
            }if(matcher==true)break;i=temp;}i++;j=0;flag=true;

        }
        if(matcher==true) {System.out.println("true");}
        else    {System.out.println("false");}
    }
    public static void main(String[] args) {

    StringMatch.getter();
    StringMatch.stringMatch();

    }
}
相关问题