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:是模式串长度 ...
随机推荐
- 修改mysql的时间/时区
# 背景 往db中insert数据发现时间不对,因为是新DB,所以猜测是mysql设置不对 # 解决方法 方法一:通过mysql命令行模式下动态修改 show variables like " ...
- phonegap3.0+HTMLl5 开发 ipad app 总结
忙碌了一段时间,终于完成了手上的这个ipad上的app ,app是用phonegap+ jquery mobile 开发的,不是用原生的objective c开发的.因为app有许多chart的渲染, ...
- python--函数名的使用,闭包,迭代器
1.函数名的使用和第一类对象 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数 函数对象可以像变量一样进行赋值,还可以作为列表的元素进行使用,可以作为返回值返回,可以作为参数进行传递 1 ...
- java学习笔记—第三方操作数据库包专门接收DataSource-dbutils (30)
Dbutils 操作数据第三方包.依赖数据源DataSource(DBCP|C3p0). QueryRunner – 接收DataSource|Connection,查询数据删除修改操作.返回结果. ...
- JSP(汇聚页)
JSP(汇聚页) ------------------------------------------------------------------------------------------- ...
- 操作mysql的指令
1,通过ip,端口,用户名,密码登陆数据 命令格式为:mysql -h ip -u root -p -P 3306例如:mysql -h 127.0.0.1 -u root -p -P 3306 2, ...
- CentOS运行C++语言的Hello World
1,编写代码,hello.cpp #include <iostream> using namespace std; int main(){ cout<<"hello ...
- 利用jaxb实现xml和bean的相互转换
1.使用jar包生成xsd文件 java -jar trang.jar a.xml a.xsd xml格式 生成的xsd文件 2.使用xjc命令生成bean文件 xjc a.xsd 生成的相关bean ...
- Using RDP to connect Windows remote desktop with Linux
安装rdesktop(一般情况下不需要这么做): sudo apt-get install rdesktop 执行连接: rdesktop xxx.xxx.xxx.xxx:3389 -u admini ...
- How to write date range query in Nest ElasticSearch client?
Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...