zoukankan      html  css  js  c++  java
  • KMP算法的JAVA实现

    package kmp;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    public class KMP {
        
        /*
         * 计算key的部分匹配表
         */
        private static Map<String,Integer>getPartialMatchTable(String str)
        {
            Map<String,Integer> result = new HashMap<String,Integer>();
            
            int length = str.length();
            
            for(int i = 1; i<=length; i++)
            {
                String partial = str.substring(0, i);
                
                int value = KMP.getPartialValue(partial);
                
                result.put(partial, value);    
            }
            return result;
        }
        
        /*
         * 计算string的部分匹配值
         */
        private static int getPartialValue(String str)
        {
            int result =0;
            
            int length = str.length();
            
            for(int i =1; i<length; i++)
            {
                if( str.substring(0,i).equals(str.substring(length-i, length)))
                    result = i;
            }
            
            return result;
        }
        
        public static ArrayList<Integer> kmp(String searchString,String key)
        {
            ArrayList<Integer> result = new ArrayList<Integer>();
            
            Map<String,Integer> partialMatchTable = KMP.getPartialMatchTable(key);
            
            int length = searchString.length();
            
            int step =1;
            
            for(int i = 0; i < length; i = i+step)            
            {
                for(int j = 0; j < key.length(); j ++)
                {
                    if(searchString.charAt(i+j) == key.charAt(j))
                    {
                        if(j == key.length()-1)
                        {
                            //如果匹配正确记录下来并step 赋值为1
                            result.add(i);
                            
                            step = 1;
                        }
                    }else{
                        //step = 已匹配的值-部分匹配值
                        step = j - partialMatchTable.get(key.substring(0, j+1));
                        
                        if(step == 0)
                        {
                            step =1;
                        }
                        break;
                    }
                }
            }
            
            
            return result;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            String temp = "ABCABC";
            
            String search = "ABABCABCHSIIAABCABCHSJDKAABCABCH";
            
            ArrayList<Integer> result = KMP.kmp(search, temp);
            
            for(Integer i:result )
            {
                System.out.println(i);
            }
    
        }
    
    }
    坚定目标,向前看
  • 相关阅读:
    linux进程间通信-共享内存
    where和having子句的区别
    多进程和多线程的区别(转载)
    android手机调试时不能打印Logcat日志信息
    来源不清,随笔
    转载
    C语言和Lua的交互
    python常用代码笔记
    python入门问题(windows7+python35+pycharm)
    常用matlab代码笔记
  • 原文地址:https://www.cnblogs.com/wangcansun/p/3494264.html
Copyright © 2011-2022 走看看