字符串匹配算法——Sunday算法和KMP算法(java版本)

1. Sunday算法

首先贴引用的博客:

http://blog.csdn.net/zy812818/article/details/52996778

下面贴java代码:

//Sunday算法
    public static boolean sunday(String destString, int lena, String patternString, int lenb){
        boolean tag = false;
        if (lena > 0 && lenb > 0){
            char[] destchars = destString.toCharArray();
            char[] patternchars = patternString.toCharArray();
            //i表示在destString的索引,j表示在patternString的索引
            int i=0,j=0;
            //<=与<符号区别,有点儿不通用,比如匹配{1,2,3,4,5,6,7}和{7},需用<=
            while(i<lena-lenb+j){
                //若当前索引位置对应字符不相等
                if(destchars[i]!=patternchars[j]){
                    //找到destchars[i+lenb-j]在patternchars的位置
                    int pos = contain_char(patternchars,destchars[i+lenb-j]);
                    if(pos==-1){
                        //没有找到
                        i = i+lenb+1-j;
                        j = 0;
                    }else{
                        i = i+lenb-pos-j;
                        j = 0;
                    }
                }
                else{
                    //此时destchars[i] == patternchars[j],判断j是都等于lenb-1,若等于,则返回true
                    if(j == lenb-1){
                        tag = true;
                        break;
                    }
                    else{
                        i++;
                        j++;
                    }
                }
            }
        }
        return tag;
    }
    private static int contain_char(char[] chars, char target) {
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] == target) {
                return i;
            }
        }
        return -1;
    }

2. KMP算法

同样先贴网址:

http://www.cnblogs.com/c-cloud/p/3224788.html

下面贴java代码:

//KMP算法
    public static boolean KMP(String destString, int lena, String patternString, int lenb){
        boolean tag = false;
        if (lena > 0 && lenb > 0 && lena >= lenb) {
            char[] destchars = destString.toCharArray();
            char[] patternchars = patternString.toCharArray();
            int[] next = next(patternchars);
            //for(int i=0;i<next.length;i++)
            // System.out.print(next[i]+" ");
            //下面i为目标串的索引,j为匹配串的索引
            for(int i=0,j=0;i<lena;i++){
                while (j>0 && patternchars[j]!=destchars[i])
                    j = next[j-1];
                if(patternchars[j]==destchars[i])
                    j++;
                if(j==lenb) {
                    tag = true;
                    //找到匹配的位置
                    //System.out.println("匹配位置为:"+(i-lenb+1));
                    break;
                }
            }
        }
        return tag;
    }
    public static int[] next(char[] patternChars){
        int lenb = patternChars.length;
        int[] next = new int[lenb];
        int q,k;
        for(q=1,k=0;q<lenb;q++){
            while(k>0 && patternChars[q]!=patternChars[k])
                k=next[k-1];
            if(patternChars[q]==patternChars[k])
                k++;
            next[q]=k;
        }
        return next;
    }
    原文作者:KMP算法
    原文地址: https://blog.csdn.net/lzc1039862438/article/details/73498384
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞