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:是模式串长度 ...
随机推荐
- vs2015上使用github进行版本控制
我是用的是vs2015企业版 一.首先创建项目,右下角选择新建git存储库 二.在工具栏选择团队-管理连接,打开团队资源管理器,点击同步 . 三.选择下面的发布选项 四.在gitgub上新建仓库,得到 ...
- 浅谈 温故知新——HTML5!
古人有云:温故而知新.活到老,学到老,作为一枚前端的程序猿,不停的学习能够让我们对我们的技术有一个更加丰富的认识.这几天,项目已经完成,但我发现自己的知识体系存在一些短板,特别是在H5方面,所以我又回 ...
- .Net Core MVC初学习
.net core已经出来很长一段时间了,没有很好的学习过,现在工作不那么忙了,参考官方文档,在这里记录自己的学习过程! ASP.NET Core 是一个跨平台的高性能开源框架,用于生成基于云且连接 ...
- sharepoint 2013 search configuration
在建立search application之前完成以下命令 $hostA = Get-SPEnterpriseSearchServiceInstance -Identity "WTCSPS0 ...
- 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- Problem I: GJJ的日常之玩游戏(GDC)
Contest - 河南省多校连萌(四) Problem I: GJJ的日常之玩游戏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 107 Solve ...
- kvm快照备份及常用命令
转载自:http://www.myjishu.com/?p=431 好文章 kvm快照备份及常用命令 kvm快照,分两种: 1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份 2种由 ...
- leecode刷题(21)-- 删除链表的倒数第N个节点
leecode刷题(21)-- 删除链表的倒数第N个节点 删除链表的倒数第N个节点 描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2- ...
- CodeChef April Challenge 2019题解
传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...
- Qt5学习笔记(消息过滤器)
T06EventFilter.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += widgets gui MyWidget.h #i ...