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:是模式串长度 ...
随机推荐
- Javascript设计模式理论与实战:组合模式
我们平时开发过程中,一定会遇到这种情况:同时处理简单对象和由简单对象组成的复杂对象,这些简单对象和复杂对象会组合成树形结构,在客户端对其处理的时候要保持一致性.比如电商网站中的产品订单,每一张产品订单 ...
- sql--CONVERT、FOR XML PATH解决实际问题
需求:每个平台分类下的门店,每个门店的名称.图片路径.评分,每个门店下的四个产品的名称.图片路径.评分 思路: 一开始门店动态化好写,用Ajax就行了.但是每个门店下面的产品,每个去请求一次查询有点不 ...
- autofac JSON文件配置
autofac是比较简单易用的IOC容器.下面我们展示如何通过json配置文件,来进行控制反转. 需要用到以下程序集.可以通过nugget分别安装 Microsoft.Extensions.Confi ...
- Visual Studio 2017 如何 监控当前变量 占用内存空间大小
在进行VS调试时 大家是否想知道当前变量 占用了内存多少空间呢 这对系统调优还是很有帮助的吧
- Vue的组件和路由
组件 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能. 组件的 ...
- 【OCP-12c】2019年CUUG OCP 071考试题库(78题)
78.View the exhibit and examine the structure of the CUSTOMERStable. Which two tasks would require s ...
- 解决修改完系统默认python版本后yum不可用的问题!!!!!!
文章转自: http://www.linuxidc.com/Linux/2013-05/84727.htm #vi /usr/bin/yum 将文件头部的: #!/usr/bin/python 改为: ...
- MongoDB 数据自动同步到 ElasticSearch
我们产品中需要全文检索的功能,后端数据存储主要使用了 MySQL + MongoDB,而其中需要检索的内容是在 MongoDB 中的. MongoDB 本身是自带文本索引功能的,但是,不支持中文.术业 ...
- TCP 和 UDP 协议
TCP 和 UDP 协议 一.socket层 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐 ...
- day 13 课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/7 18:00# @Author : Endless-cloud# @Site : # @File : day 13 课 ...