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

直接接上篇上代码:
//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的更多相关文章
- Java数据结构之字符串模式匹配算法---KMP算法
本文主要的思路都是参考http://kb.cnblogs.com/page/176818/ 如有冒犯请告知,多谢. 一.KMP算法 KMP算法可以在O(n+m)的时间数量级上完成串的模式匹配操作,其基 ...
- Java数据结构之字符串模式匹配算法---Brute-Force算法
模式匹配 在字符串匹配问题中,我们期待察看源串 " S串 " 中是否含有目标串 " 串T " (也叫模式串).其中 串S被称为主串,串T被称为子串. 1.如果在 ...
- 《数据结构》之串的模式匹配算法——KMP算法
//串的模式匹配算法 //KMP算法,时间复杂度为O(n+m) #include <iostream> #include <string> #include <cstri ...
- 串的模式匹配算法 ------ KMP算法
//KMP串的模式匹配算法 #include <stdio.h> #include <stdlib.h> #include <string.h> int* get_ ...
- [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 转载自:http://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boy ...
- 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 本文内容框架: §1 Boyer-Moore算法 §2 Horspool算法 §3 Sunday算法 §4 ...
- 数据结构- 串的模式匹配算法:BF和 KMP算法
数据结构- 串的模式匹配算法:BF和 KMP算法 Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...
- 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法
ref : https://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boyer-Moore算法 §2 Horspool算法 §3 Sunday算法 §4 KMP ...
- 字符串匹配算法——KMP算法
处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...
随机推荐
- 开源战棋 SLG 游戏框架设计思考(一)简介和游戏引擎
战棋 SLG 游戏 SLG(Simulation Game)游戏是模拟游戏的简称.战棋类的SLG有两种:一种是 War Game 中的兵棋推演分支,常见的游戏有战争艺术3(TOAW3 — The Op ...
- Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>
Collection<E>接口extends Iteratable<E>接口. Iteratable<T>:实现接口使得对象能够成为“for-each loop”的 ...
- iTop Webservice列表
{ u'operations':[ { u'verb':u'core/create', u'description':u'Create an object', u'extension':u'CoreS ...
- sweetalert api中文开发文档和手册
官网和下载地址: http://t4t5.github.io/sweetalert/ 2016年10月30日14:10:21 废话,目前php开发越来越API话,所以php方法很多都是json返回数 ...
- find_elements后点击不了抓取的元素
1.莫名其妙抓不到元素,要去看句柄,是不是没有切换 h=driver.current_window_handle nh=driver.window_handles for i in nh: if i! ...
- CPP - sort
#include "stdafx.h" #include <iostream> #include <string> using namespace std; ...
- winsock error 相关
10061-WSAECONNREFUSED 是指没有启动服务器或者说服务器没有处于监听状态.通常导致client在connect时候返回这个错误码的原因在于服务端与客户端设置的端口号没有同步转换导致( ...
- 如何解决Visual Studio调试Debug很卡很慢
http://brightguo.com/make-debugging-faster-with-visual-studio/ Have you ever been frustrated by slow ...
- magento后台paypal设置
如何在magento后台设置paypal呢? 这边把整理的简单跟大家分享一下. 1.system->config-paypel1.1 Merchant Country 设置国家1.2 Email ...
- QT 调用VS2015编写的Dll
最近在用QT调用VC生成的库,QT使用的是MinGW调试器,出现与动态库不兼容的问题,最后发现QT只能识别VC生成的C格式下的动态库 也就是在导入导出设置时加入extern "C" ...