本文共 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/