您当前的位置: 首页 >  算法

xiangzhihong8

暂无认证

  • 3浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

KMP算法

xiangzhihong8 发布时间:2016-04-07 15:12:17 ,浏览量:3

KMP为的是解决2字符串匹配问题的算法,检查一个字符串是否为另一个的子串,sub = "abc" , str = "aabcd" ,str里包含了一个sub,KMP算法可以以O(M+N)的复杂度找到子串在str的位置。

那代码怎么实现呢:

public class Kmp {
	
	public static void main(String[] args) {
		 String str = "abbabbbbcab";   
	     String sub = "bbcab"; 
	     char[] s=str.toCharArray();
	     char[] t=sub.toCharArray();
	    System.out.println("s包含t的位置"+KMP_Index(s, t)); 
	}
	
	/**
	 * @param s
	 * @param t
	 * @return 匹配成功 返回模式串在主串中的头下标,匹配失败返回-1  
	 */
	 public static int KMP_Index(char[] s, char[] t) {  
	        int[] next = next(t);  
	        int i = 0;  
	        int j = 0;  
	        while (i             
关注
打赏
1482932726
查看更多评论
0.1937s