kmp算法 の 初体验
来源:互联网 发布:javascript事件委托 编辑:程序博客网 时间:2024/06/09 15:30
KMP算法好难呀~!
看了一遍书,不懂,
看了一遍博客,没完全懂,
然后又去看了一个讲解视频,有点儿懂了,
再自己整理了一下笔记,读了几遍代码,算是懂了?
这种只可意会不可言传的感觉,我算是懂了。
懒得打字了,把自己做的笔记传上来吧:
/** * */package dianer;/** * * @author LilyLee * @date 2017年3月19日 * @Version *问题描述: KMP算法的核心是求next数组。 */public class KMPTest { public static void main(String[] args){ // KMPTest ktest=new KMPTest("BBC ABCDAB ABCDABCDABDE", "ABCDABD"); KMPTest ktest=new KMPTest("BBABCDABDB ABCDABCDABDE", "ABCDABD"); ktest.debugNextArr(); int theLoc=ktest.getIndexOfStr(); System.out.println(); System.out.println("匹配位置在:"+theLoc); } private int[] _nextArr=null; private String _originStr=null; private String _moduleStr=null; private int[] _resultArr=null; public KMPTest(String originStr,String moduleStr){ _originStr=originStr; _moduleStr=moduleStr; _nextArr=caculate_nextArr(); } /** * 计算next 数组的值。 * */ private int[] caculate_nextArr(){ if(_moduleStr==null||_moduleStr.length()==0){ return null; } int[] theNextArr=new int[_moduleStr.length()]; for(int i=0;i<_moduleStr.length();i++){ if(i==0){ theNextArr[i]=-1; } else if(i==1){ if(_moduleStr.charAt(0)==_moduleStr.charAt(1)){ theNextArr[i]=1; }else{ theNextArr[i]=0; } } else{ int theLength2=i; boolean hasEqual=false; for(int j=theLength2-1;j>=0;j--){ String prefix_str=_moduleStr.substring(0, j+1); String suffix_str=_moduleStr.substring(theLength2-j,theLength2+1); if(prefix_str.endsWith(suffix_str)){ hasEqual=true; theNextArr[i]=prefix_str.length(); break; } else{ } } if(hasEqual==false){ theNextArr[i]=0; } } } //--- return theNextArr; } public void debugNextArr(){ if(_nextArr!=null){ System.out.println("next array的值:"); for(int tmp:_nextArr){ System.out.print(tmp+" "); } } } public int getIndexOfStr(){ if(_moduleStr==null||_moduleStr.length()<=0){ return -1; } if(_originStr==null||_originStr.length()<=0){ return -1; } if(_originStr.length()<_moduleStr.length()){ return -1; } int res=-1; int totalLength=_originStr.length(); boolean flag_end=false; int origin_loc=0; int module_loc=0; while(flag_end==false){ char c_origin=_originStr.charAt(origin_loc); char c_module=_moduleStr.charAt(module_loc); boolean needtoGoOn=true; int childLoc=1; if(c_origin==c_module){ if(module_loc==_moduleStr.length()-1){ res=origin_loc-module_loc; break; } else{ origin_loc++; module_loc++; } } else{ if(module_loc==0){ origin_loc++; module_loc=0; if(origin_loc>=totalLength){ break; } } else{ if(module_loc<=0){ module_loc++; origin_loc++; } else{ int m_callback=_nextArr[module_loc-1]; module_loc=m_callback;} } continue; } if(origin_loc>=totalLength){ break; } } return res; } }
0 0
- kmp算法 の 初体验
- KMP算法初体验
- KMP算法体验
- 算法初体验-1
- 算法初体验-2
- 算法初体验-3
- prim算法初体验
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- 粒子群算法初体验
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- 机器学习算法 原理、实现与实践 —— 感知机与梯度下降
- g++版本太低了,连iostream都不支持
- Ubuntu16.04安装有道词典(包含第三方软件问题解决方法)
- 坐标离散化,imos
- 多线程编程
- kmp算法 の 初体验
- python3.5 连接mysql本地数据库
- 图小零,一个有点智能的机器人
- 奇怪的捐赠
- 计算机基础
- PAT (Advanced Level) Practise 1001. A+B Format (20)
- 【九度OJ】题目1193:矩阵转置 解题报告
- Codeforces 599D Spongebob and Squares【思维枚举+数学方程】
- 《ACM程序设计》书中题目--problem y