kmp java implement--转
来源:互联网 发布:配置网络 ubuntu 编辑:程序博客网 时间:2024/05/01 09:28
http://cs.nyu.edu/~yap/classes/basic/progs/patternMatching/KMP.java
/** * @file KMP.java * @synopsis An implementation of KMP matching, derived from * a modification of the Match.java program. * Again, the goal is to find an occurrence of a pattern P in a text T. * CONVENTION: * The first index (i=0) of arrays are not used. * * @author Chee Yap * @date Apr 25, 2001 (for Basic Algorithms class) */public class KMP { // Members: char[] T; // This is the text char[] P; // This is pattern int [] fail; // Failure function for pattern // Constructors: KMP(char[] p, char[] t) { P = p; T = t; computeFail(); } // // Methods: /****************************************************** Routine to compute the failure function ******************************************************/ public void computeFail() { // init: fail = new int[P.length]; fail[1] = 0; // loop: for (int k=2; k< fail.length; k++) { int kk = fail[k-1]; while (kk>0 && (P[kk] != P[k-1])) kk = fail[kk]; fail[k] = 1 + kk; } } // computeFail(P) /****************************************************** THIS IS THE MAIN ROUTINE ******************************************************/ public int find(int start) { // init: int j = start; // text index int k = 1; // pattern index // loop: while (j < T.length) { if (k >= P.length) return(j - k + 1); if ((T[j] == P[k]) || (k==0)) { j++; k++; } else { k = fail[k]; // k could become 0 } } // while // Not found: return(-1); } // find() /****************************************************** prints data ******************************************************/ void output() { System.out.print("> Pattern = \""); for (int i=1; i< P.length; i++) System.out.print(P[i]); System.out.print("\"\n> Text = \""); for (int i=1; i< T.length; i++) System.out.print(T[i]); System.out.println("\""); } // output() /****************************************************** Main method ******************************************************/ public static void main( String[] args) { // sample input with 6 keys // (the first is a dummy key) // char[] p = {'0', 'o', 'u'}; char[] p = {'0', 'y', 'o', 'u'}; char[] t = {'0', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ' ', 'a', ' ', 'y', 'o', 'u', 't', 'h', '?'}; // construct a KMP object KMP m = new KMP(p, t); m.output(); // print data // find all matches int f = m.find(1); if (f<1) System.out.println(">> No match found"); else { while (f>=1) { System.out.println(">> Match found at position " + f); f = m.find(f+1); }//while }//else } // main}//class KMP
0 0
- kmp java implement--转
- LeetCode Implement strStr() --- KMP
- Implement strStr() [LeetCode] + KMP
- KMP 算法 && Implement strStr()
- Implement strStr() && kmp算法
- Implement strStr() KMP算法
- kmp-----leetcode(Implement strStr() )
- 28. Implement strStr()(KMP)
- 【KMP&字符串匹配】Implement strStr()
- 28. Implement strStr()(KMP算法)
- Java中extends 与 implement 区别(转)
- Java中extends 与 implement 区别(转)
- Implement strStr() 字符串匹配(KMP)
- [Leetcode] Implement strStr()与 KMP算法
- 【leetcode KMP算法实现】Implement strStr()
- *****(leetcode_string) Implement strStr() (KMP算法*****)
- Leetcode 28. Implement strStr() KMP算法解题
- LeetCode Implement strStr(kmp或者BM)
- Hudson-ci/Using Hudson/Installing Hudson/Installing Hudson RPM--官方文档
- 深入redis内部--实现双向链表
- 深入redis内部--实现字符串
- 深入redis内部--字典实现(未完成)
- 2013年小结及2014年展望
- kmp java implement--转
- java实现迷宫算法--转
- Memcached分布式算法详解--转
- 设计模式之原型模式
- 【Cracking the coding interview】Q1.1(字符唯一)
- Unity屏幕渲染技术
- Oracle中出现Ora-00054该怎么处理,V$locked_object和V$session的用法
- TS流的基本概念
- secureCRT下的串口不能输入