如何在大量的字符找是否有匹配的?

    public static boolean match(String stringS, String stringT) {
        char[] charsS = stringS.toCharArray();
        char[] charsT = stringT.toCharArray();
        int[] next = getNextPlus(stringT);
        int j = 0;
        for (int i = 0, sizeI = charsS.length; i < sizeI; i ++) {
            if (0 > j || charsT[j] == charsS[i]) {
                j ++;
            } else {
                j = next[j];
                i --;
            }
            if (charsT.length == j) {
                print("-----: "+stringS.substring(j));
                return true;
            }
        }
        return false;
    }

    private static int[] getNextPlus(String stringT) {
        char[] chars = stringT.toCharArray();
        int[] next = new int[chars.length];
        next[0] = -1;
        for (int i = 1; i < chars.length; i ++) {
            int j = next[i - 1];
            while (true) {
                if (-1 == j || chars[i - 1] == chars[j]) {
                    next[i] = j + 1;
                    break;
                } else {
                    j = next[j];
                }
            }
        }
        for (int i = 0; i < next.length; i ++) {
            if (0 <= next[i] && chars[next[i]] == chars[i]) {
                next[i] = next[next[i]];
            }
        }
        return next;
    }

你可能感兴趣的:(计算题)