Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用通配符实现Boyer-Moore-Horsepool算法_Java_Algorithm_Full Text Search_Wildcard_Boyer Moore - Fatal编程技术网

Java 用通配符实现Boyer-Moore-Horsepool算法

Java 用通配符实现Boyer-Moore-Horsepool算法,java,algorithm,full-text-search,wildcard,boyer-moore,Java,Algorithm,Full Text Search,Wildcard,Boyer Moore,我想实现Boyer-Moore-Horspool算法的泛化,该算法考虑通配符(匹配单词中的任何字母)。这意味着模式h\use将在本文中找到两次:horsehouse 我需要帮助来实现这一点,我不能得到足够深入的理解算法,以解决这个问题,我自己,一些提示 int [] createBadCharacterTable(char [] needle) { int [] badShift = new int [256]; for(int i = 0; i < 2

我想实现Boyer-Moore-Horspool算法的泛化,该算法考虑通配符(匹配单词中的任何字母)。这意味着模式
h\use
将在本文中找到两次:
horsehouse

我需要帮助来实现这一点,我不能得到足够深入的理解算法,以解决这个问题,我自己,一些提示

int [] createBadCharacterTable(char [] needle) {

        int [] badShift = new int [256];

        for(int i = 0; i < 256; i++) {
            badShift[i] = needle.length;
        }

        int last = needle.length - 1;

        for(int i = 0; i < last; i++) {
            badShift[(int) needle[i]] = last - i;
        }

        return badShift;
    }

    int boyerMooreHorsepool(String word, String text) {

        char [] needle = word.toCharArray();
        char [] haystack = text.toCharArray();

        if(needle.length > haystack.length) {
            return -1;
        }

        int [] badShift = createBadCharacterTable(needle);

        int offset = 0;
        int scan = 0;
        int last = needle.length - 1;   
        int maxoffset = haystack.length - needle.length;

        while(offset <= maxoffset) {
            for(scan = last; (needle[scan] == haystack[scan+offset] ||  needle[scan] == (int) '_'); scan--) {

                if(scan == 0) { //Match found
                    return offset;
                }
            }
            offset += badShift[(int) haystack[offset + last]];
        }
        return -1;
    }
int[]createBadCharacterTable(字符[]指针){
int[]badShift=新int[256];
对于(int i=0;i<256;i++){
badShift[i]=针的长度;
}
int last=针的长度-1;
for(int i=0;i干草堆.长度){
返回-1;
}
int[]badShift=createBadCharacterTable(指针);
整数偏移=0;
int扫描=0;
int last=针的长度-1;
int maxoffset=haystack.length-pinder.length;

while(offset)你有一个特定的问题吗?编辑:添加了使用u40;下划线)作为通配符的解决方案。我刚刚在for-loop中添加了needle[scan]==(int)41;作为测试。这是作业吗?2013年11月,我发现有人再次发布了这个问题!(删除之前)如果没有,那么您不需要打开它的代码-所以只需使用
Pattern.compile(Pattern.quote(needle.replaceAll(“\u”,“\\\\ E.\\\\Q”);