KMP算法
来源:互联网 发布:淘宝店铺布局管理 编辑:程序博客网 时间:2024/05/21 14:51
package com.abstractdatatype.string;/*字符串匹配的KMP算法*/public class StringMatch_KMP {public static void main(String[] args) {String s = "abbabbbbcab"; // 主串 String t = "bbcab"; // 模式串 // char[] ss = s.toCharArray(); char[] tt = t.toCharArray(); int [] next=next(tt); for (int i : next) {System.out.print(i+" ");} // System.out.println(KMP_Index(ss, tt)); // KMP匹配字符串 } /** * 获得字符串的next函数值 * * @param t * 字符串 * @return next函数值 */ public static int[] next(char[] t) { int[] next = new int[t.length]; next[0] = -1; int i = 0; int j = -1; while (i < t.length - 1) { if (j == -1 || t[i] == t[j]) { i++; j++; if (t[i] != t[j]) { next[i] = j; } else { next[i] = next[j]; } } else { j = next[j]; } } return next; } /** * KMP匹配字符串 * * @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 <= s.length - 1 && j <= t.length - 1) { if (j == -1 || s[i] == t[j]) { i++; j++; } else { j = next[j]; } } if (j < t.length) { return -1; } else return i - t.length; // 返回模式串在主串中的头下标 } }
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- 阿里云cenos 6.5 模板上安装 docker
- UVA 12108
- 最简单的Phoenix 访问 HBase例子
- spring mvc数据绑定时通过去除html标签防止js注入
- PB50最受欢迎博客评选领奖品通知
- KMP算法
- 1.1 polynomial curve fitting
- spring缓存集成memcache
- 深入浅出 - Android系统移植与平台开发(十) - led HAL简单设计案例分析
- CodeForces - 1B Spreadsheets
- 分享我的2014年3月unity3d面试题与参考答案
- 遍历问题[CODEVS1029]解题报告
- 关于全排列 leetcode
- ipa 打包