/**
 * 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));;
        
    }
}

最新文章

  1. 初见SpringMVC
  2. Python学习笔记——条件和循环
  3. 2. ProGit-Git基础
  4. VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程
  5. github上所有项目的受欢迎程度排名,包括超大型项目
  6. adb开启不了解决方案
  7. MongoDB自学笔记2---1.2 初识MongoDB
  8. unix ourhdr.h myerr.h
  9. D3.js:力导向图
  10. swfobject.embedSWF属性与用法
  11. 算法模板——计算几何2(二维凸包——Andrew算法)
  12. 修改Tomcat访问的端口号
  13. PBRT笔记(13)——光线传播1:表面反射
  14. python--继承--方法的重写---和父类的扩展
  15. OpenStack控制节点上搭建Q版keystone服务(step3)
  16. mpvue 初体验之改写车标速查小程序
  17. SNMP扫描
  18. PythonStudy——数据类型总结 Data type summary
  19. Java 学习札记(三)免安装版TomCat中tomcat6w.exe的运行
  20. 263A

热门文章

  1. 使用XStream注解实现Java对象与XML互相转换的代码示例
  2. iOS:编译错误Undefined symbols for architecture i386: _OBJC_CLASS_$_XXX&amp;quot;, referenced from: error
  3. Android 连接 SQL Server (jtds方式)——上
  4. jquery $(&#39;#btn&#39;).click与$(&quot;#btn&quot;).live(&quot;click&quot;,function()有什么区别?
  5. 常用布局,div竖直居中
  6. LinkButton和HyperLink的页面跳转用法
  7. WPF DataGrid 绑定DataSet数据 自动生成行号
  8. 【转】Objective-C中一种消息处理方法performSelector: withObject:
  9. 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中
  10. centos 给鼠标右击添加 “打开终端” 菜单项