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

kmp java implement--转的更多相关文章

  1. java implement

    接口不能被实例化,但是可以声明一个接口类型的变量. eg. A implements B,则可以有B variableName = new A(),这和extends的用法是类似的 接口可被认为是纯抽 ...

  2. Android Touch事件相关源码【Android SourceCode 2.3.6】

    2018-05-31 17:23:46 Note: 这里的源码来自Android 2.3.6,这个版本的代码比较简单,适合理解Touch事件的传递原理.后续版本源码复杂了很多,但是原理都是类似的. 2 ...

  3. Substring Search

    查找子字符串 Introduction 在长度为 N 的文本里寻找长度为 M 的模式(子串),典型情况是 N >> M. 这个应用就很广泛啦,在文本中寻找特定的模式(子串)是很常见的需求. ...

  4. Pressed状态和clickable,duplicateParentState的关系

    做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...

  5. spring注解开发-容器创建全过程(源码)

    1.Spring容器的创建会经历refresh()方法[创建刷新](以AnnotationConfigApplicationContext为例) public AnnotationConfigAppl ...

  6. 【朝花夕拾】Android自定义View篇之(一)View绘制流程

    前言 转载请申明转自[https://www.cnblogs.com/andy-songwei/p/10955062.html]谢谢! 自定义View.多线程.网络,被认为是Android开发者必须牢 ...

  7. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  8. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. KMP算法-Java实现

    目的: 为了解决字符串模式匹配 历程: 朴素模式匹配:逐次进行比较 KMP算法:利用匹配失败得到的信息,来最大限度的移动模式串,以此来减少比较次数提高性能 概念: m:是目标串长度 n:是模式串长度 ...

随机推荐

  1. Maven Compilation error [package org.testng.annotations does not exist]

    背景 在执行mvn test的时候,提示package org.testng.annotations does not exist 解决办法 Open pom.xml file. Go to &quo ...

  2. shell中调用jenkins API批量运行历史任务

    shell中调用jenkins API批量运行jenkins带参数的任务: #!/bin/sh #startdate=20150127 startdate=20150201 while [ " ...

  3. C# print pos winform

    先将pos机设置为默认 控制面板->打印机和传真->右键->服务器属性 首先创建 ClassPrint 对象 using System; using System.Drawing; ...

  4. ListOrderedMap与Map的区别

    ListOrderedMap位于commons-collections的jar包里,与普通的map相比,ListOrderedMap的key可保持原有顺序,在某些情况比如做报表的时候,非常实用. 代码 ...

  5. jquery源码解析:jQuery数据缓存机制详解1

    jQuery中有三种添加数据的方法,$().attr(),$().prop(),$().data().但是前面两种是用来在元素上添加属性值的,只适合少量的数据,比如:title,class,name等 ...

  6. Linux动态库的导出控制

    在实际工作中,许多软件模块是以动态库的方式提供的.做为模块开发人员,我们不仅要掌握如何编写和构建动态库,还要了解如何控制动态库的导出接口,这样,我们可以向模块的用户仅导出必要的接口,而另一些内部接口, ...

  7. 调用jdbc已经写成的方法----jdbc工具类抽取方式二

    先创建db.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/web08?useUnicode=true& ...

  8. 总结day2 ---- while循环的简单使用, 格式化输出.运算符.以及编码的应用

    内容提要 一 : while 循环 while 的基本语句操作 如何终止循环 二 :格式化输出 三 :运算符号 四 :编码初识别 一 : while 循环 1  >>>>whi ...

  9. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  10. Bootstrap-datepicker日期时间选择器的简单使用

    日期时间选择器 目前,bootstrap有两种日历.datepicker和datetimepicker,后者是前者的拓展. Bootstrap日期和时间组件: 使用示例: 从左到右依次是十年视图.年视 ...