kmp java implement--转
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--转的更多相关文章
- java implement
接口不能被实例化,但是可以声明一个接口类型的变量. eg. A implements B,则可以有B variableName = new A(),这和extends的用法是类似的 接口可被认为是纯抽 ...
- Android Touch事件相关源码【Android SourceCode 2.3.6】
2018-05-31 17:23:46 Note: 这里的源码来自Android 2.3.6,这个版本的代码比较简单,适合理解Touch事件的传递原理.后续版本源码复杂了很多,但是原理都是类似的. 2 ...
- Substring Search
查找子字符串 Introduction 在长度为 N 的文本里寻找长度为 M 的模式(子串),典型情况是 N >> M. 这个应用就很广泛啦,在文本中寻找特定的模式(子串)是很常见的需求. ...
- Pressed状态和clickable,duplicateParentState的关系
做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...
- spring注解开发-容器创建全过程(源码)
1.Spring容器的创建会经历refresh()方法[创建刷新](以AnnotationConfigApplicationContext为例) public AnnotationConfigAppl ...
- 【朝花夕拾】Android自定义View篇之(一)View绘制流程
前言 转载请申明转自[https://www.cnblogs.com/andy-songwei/p/10955062.html]谢谢! 自定义View.多线程.网络,被认为是Android开发者必须牢 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- KMP算法-Java实现
目的: 为了解决字符串模式匹配 历程: 朴素模式匹配:逐次进行比较 KMP算法:利用匹配失败得到的信息,来最大限度的移动模式串,以此来减少比较次数提高性能 概念: m:是目标串长度 n:是模式串长度 ...
随机推荐
- EAS_AOP分布式事务
在System.Transactions事务体系中,为事务提供了7种不同的隔离级别.这7中隔离级别分别通过 System.Transactions.IsolationLevel的7个枚举项表示. pu ...
- c#设计模式之装饰器模式(Decorator Pattern)
引子 在面向对象语言中,我们常常会听到这样一句话:组合优于继承.那么该如何去理解这句话呢? 下面我将以游戏装备为模型用简单的代码去展示它 先创建一个装备的抽象类,然后创建刀枪2个具体的业务子类 pub ...
- SharePoint Caml Query 使用
需要注意的是: 再使用ListQueryWebPart部件时,默认查看的list列表会出现乱码的情况,需要开启服务器呈现模式,如图: 特此记录一下
- 【题解】 AGC029-A Irreversible operation
传送门 定位:思维好题. 考虑无论如何每一个W都会和前面的B在一起交换一次,所以直接求和就好了. 注意long long的使用. #include<stdio.h> #include< ...
- 销售系统项目业务分析和Java中使用邮箱
项目一般大致可分为三个模块, 我们以销售系统为例 分为 基础模块 进货模块 财务模块三个 基础模块分为:权限模块 产品模块和基础代码,基础模块的设计十分重要会影响到整个项目, 代码较为简单 核心模块 ...
- opencv3.3 安装环境教程以及实现个图片读取功能
一.opencv3.3安装环境 1. 首先要安装这个opencv3.3,我们必须要安装一个python (IDLE:集成开发环境),我自己安装了IDLE3.6和pycharm2017,这些软件都可以去 ...
- kali linux之msf后渗透阶段
已经获得目标操作系统控制权后扩大战果 提权 信息收集 渗透内网 永久后门 基于已有session扩大战果 绕过UAC限制 use exploit/windows/local/ask set sessi ...
- Ajax请求参数解释
ajax常用的两个请求是get和post,而springmvc的控制层可以接收ajax请求. 但是这个过程非常灵活,变化很大,容易出错. $.ajax({ url : SITE_PATH + &quo ...
- [ActionScript 3.0] 模拟win7彩色气泡屏保效果
主文件: package { import com.views.BubbleView; import com.views.ColorfulBubble; import flash.display.Sp ...
- Class文件分析
java源码和class文件 源码: package classloader; public class TestCase { private int value; public int test() ...