直接接上篇上代码:

 //KMP算法
public class KMP { // 获取next数组的方法,根据给定的字符串求
public static int[] getNext(String sub) { int j = 1, k = 0;
int[] next = new int[sub.length()];
next[0] = -1; // 这个是规定
next[1] = 0; // 这个也是规定
//
while (j < sub.length() - 1) {
if (sub.charAt(j) == sub.charAt(k)) {
next[j + 1] = k + 1;
j++;
k++;
} else if (k == 0) {
next[j + 1] = 0;
j++;
} else {
k = next[k];
} }
return next;
} // 根据给定的主串和子串,采用KMP算法来获取模式匹配
public static int kmp(String src, String sub) { // 首先生成模式串sub的next[j]
int[] next = getNext(sub);
int i = 0, j = 0, index = -1;
while (i < src.length() && j < sub.length()) {
if (src.charAt(i) == sub.charAt(j)) {
i++;
j++;
} else if (j == 0) {
i++;
} else {
j = next[j];
}
} // 得到开始匹配的位置索引
if (j == sub.length()) {
index = i - sub.length();
}
return index;
}
}

//

 //KMP算法的测试
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String src = "aaaaaaab";
String sub = "ABCDABDABD";
int[] next = KMP.getNext(sub);
//int[] next = lengthKMP(sub.toCharArray());
for (int i : next) {
System.out.print(i + " ");// -1 0 1 2 3
} // System.out.println();
// System.out.println(KMP.kmp(src, sub));
} //补充上一篇中的对于前缀后缀的讨论的获取部分匹配数组的算法
public static int[] lengthKMP(char[] mchar) {
int[] fixNum = new int[mchar.length];
for (int i = 1, j = 0; i < mchar.length; i++) {
if (mchar[j] == mchar[i]) {
fixNum[i] = j + 1;
j++;
} else if (j > 0) {
j = 0;
i -= j;
}
}
// return [0, 0, 0, 0, 1, 2, 0, 1, 2, 0]ABCDABDABD
return fixNum;
}
}

Java数据结构之字符串模式匹配算法---KMP算法2的更多相关文章

  1. Java数据结构之字符串模式匹配算法---KMP算法

    本文主要的思路都是参考http://kb.cnblogs.com/page/176818/ 如有冒犯请告知,多谢. 一.KMP算法 KMP算法可以在O(n+m)的时间数量级上完成串的模式匹配操作,其基 ...

  2. Java数据结构之字符串模式匹配算法---Brute-Force算法

    模式匹配 在字符串匹配问题中,我们期待察看源串 " S串 " 中是否含有目标串 " 串T " (也叫模式串).其中 串S被称为主串,串T被称为子串. 1.如果在 ...

  3. 《数据结构》之串的模式匹配算法——KMP算法

    //串的模式匹配算法 //KMP算法,时间复杂度为O(n+m) #include <iostream> #include <string> #include <cstri ...

  4. 串的模式匹配算法 ------ KMP算法

    //KMP串的模式匹配算法 #include <stdio.h> #include <stdlib.h> #include <string.h> int* get_ ...

  5. [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽

    字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 转载自:http://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boy ...

  6. 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽

    字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 本文内容框架: §1 Boyer-Moore算法 §2 Horspool算法 §3 Sunday算法 §4 ...

  7. 数据结构- 串的模式匹配算法:BF和 KMP算法

      数据结构- 串的模式匹配算法:BF和 KMP算法  Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...

  8. 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法

    ref : https://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boyer-Moore算法 §2 Horspool算法 §3 Sunday算法 §4 KMP ...

  9. 字符串匹配算法——KMP算法

    处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...

随机推荐

  1. 2.线性表-Linked list

    fatal.h #include <stdio.h> #include <stdlib.h> #define Error( Str ) FatalError( Str ) #d ...

  2. spring mvc@RequestParam根据参数名获取传入参数值

    在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...

  3. mysql优化limit分页

  4. static{ }语句块详解

    static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法.举ge例子: public class Test { public static i ...

  5. 重新梳理HTML基础知识

    缘起 HTML(HyperText Markup Language超文本标记语言)是用于构建web页面的标记语言和通用标准.它并不是一项新的发明,因为超文本(具有超链接的文本)和标记语言(用于电子文档 ...

  6. Netty In Action

    1 introduction 1.2 Asynchronous by design two most common ways to work with or implement an asynchro ...

  7. Xshell远程连接工具

    下载地址:http://rj.baidu.com/soft/detail/15201.html?ald Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft ...

  8. golang 环境一键式配置

    在window下,通过以下bat,自动设置环境变量,启动终端,并cd到gopath目录 set goroot=c:\go set gopath=d:\go @start "start gol ...

  9. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  10. Oracle --获取绑定变量的值.

    SELECT * FROM DBA_HIST_SQLBIND WHERE SNAP_ID>67073 AND SNAP_ID<=67079 AND SQL_ID='3DR3410F086P ...