博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串匹配算法KMP算法
阅读量:4144 次
发布时间:2019-05-25

本文共 981 字,大约阅读时间需要 3 分钟。

字符串匹配算法

方法1 是使用暴力方法

方法2 是使用KMP算法

import java.util.Scanner;/** * @author xiaohao 	* @date 创建时间:Jul 29, 2017 4:05:17 PM * @version 1.0   */public class SubstringSearch {	public static void main(String[] args) {		// TODO Auto-generated method stub		Scanner in = new Scanner(System.in);		while(in.hasNext()){			String text=in.nextLine();			String pattern=in.nextLine();						System.out.println(KMP(text,pattern));			}	}	/*	 * 暴力方法,时间复杂度 n*m	 */	public static boolean hasSubstring(String text,String pattern){		char pShortString[]=pattern.toCharArray();		char pLongString[]=text.toCharArray();		int n=pLongString.length;		int m=pShortString.length;		for(int i=0;i<=n-m;i++)		{			int j=0;//每次循环都从模式串pattern的0位置开始比较			while(j
参考

https://www.bilibili.com/video/av3246487/?from=search&seid=5075218438614903583

https://www.bilibili.com/video/av11866460/?from=search&seid=5075218438614903583

https://github.com/mission-peace/interview/blob/master/src/com/interview/string/SubstringSearch.java

转载地址:http://yhbti.baihongyu.com/

你可能感兴趣的文章
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>
性能扩展问题要趁早
查看>>
MySQL-数据库、数据表结构操作(SQL)
查看>>
OpenLDAP for Windows 安装手册(2.4.26版)
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>