Java实现KMP算法
/**
* Java实现KMP算法
*
* 思想:每当一趟匹配过程中出现字符比较不等,不需要回溯i指针,
* 而是利用已经得到的“部分匹配”的结果将模式向右“滑动”尽可能远
* 的一段距离后,继续进行比较。
*
* 时间复杂度O(n+m)
*
*/
public class KMP {
//通过计算返回字串t的next数组
public int[] get_next(char[] t){
int lengthT = t.length;
int[] next = new int[lengthT];
next[0] = -1;
int i = 0;
int j = -1;
while(i < lengthT-1){
//t.charAt(i)表示后缀的单个字符,t.charAt(j)表示前缀的单个字符
if(j == -1 || t[i] == t[j]){
++i;
++j;
next[i] = j;
}else{
//若字符不相同,则j值回溯
j = next[j];
}
}
return next;
}
//改进版求next
public int[] get_nextval(char[] t){
int[] nextval = new int[t.length];
nextval[0] = -1;
int i = 0;
int j = -1;
while(i < t.length - 1){
if(j == -1 || t[i] == t[j]){
++i;
++j;
if(t[i] != t[j]){
nextval[i] = j;
}else{
nextval[i] = nextval[j];
}
}else{
j = nextval[j];
}
}
return nextval;
}
//s是主串,t是子串,匹配成功返回下标,匹配不成功返回-1
public int kmp_Index(char[] s, char[] t){
int[] next = get_nextval(t);
int i = 0;
int j = 0;
while(i <= s.length - 1 && j <= t.length - 1){
if(j == -1 || s[i] == t[j]){
++i;
++j;
}else{
j = next[j];
}
}
if(j < t.length){
return -1;
}else{
return i - t.length;
}
}
public static void main(String[] args) {
KMP kmp = new KMP();
String s = "abbabbbbcab";
String ss = "babbb";
char[] s1 = s.toCharArray();
char[] ss1 = ss.toCharArray();
System.out.println(kmp.kmp_Index(s1, ss1));;
}
}
最新文章
- 初见SpringMVC
- Python学习笔记——条件和循环
- 2. ProGit-Git基础
- VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程
- github上所有项目的受欢迎程度排名,包括超大型项目
- adb开启不了解决方案
- MongoDB自学笔记2---1.2 初识MongoDB
- unix ourhdr.h myerr.h
- D3.js:力导向图
- swfobject.embedSWF属性与用法
- 算法模板——计算几何2(二维凸包——Andrew算法)
- 修改Tomcat访问的端口号
- PBRT笔记(13)——光线传播1:表面反射
- python--继承--方法的重写---和父类的扩展
- OpenStack控制节点上搭建Q版keystone服务(step3)
- mpvue 初体验之改写车标速查小程序
- SNMP扫描
- PythonStudy——数据类型总结 Data type summary
- Java 学习札记(三)免安装版TomCat中tomcat6w.exe的运行
- 263A
热门文章
- 使用XStream注解实现Java对象与XML互相转换的代码示例
- iOS:编译错误Undefined symbols for architecture i386: _OBJC_CLASS_$_XXX&;quot;, referenced from: error
- Android 连接 SQL Server (jtds方式)——上
- jquery $(&#39;#btn&#39;).click与$(";#btn";).live(";click";,function()有什么区别?
- 常用布局,div竖直居中
- LinkButton和HyperLink的页面跳转用法
- WPF DataGrid 绑定DataSet数据 自动生成行号
- 【转】Objective-C中一种消息处理方法performSelector: withObject:
- 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中
- centos 给鼠标右击添加 “打开终端” 菜单项