字符串匹配问题:KMP算法
来源:互联网 发布:数据库编辑器有哪些 编辑:程序博客网 时间:2024/05/23 18:03
传统字符串匹配和KMP算法
转载时请注明来源:http://blog.csdn.net/ccfeng2008
JAVA源码如下:
/* * @class StringMatching.java * @author ccfeng * @date 2013-10-5 * * */package kmp;import static java.lang.System.out;import java.util.ArrayList;import java.util.List;public class StringMatching {public static int traditional(char[] targets, char[] patterns) {if (targets == null || targets.length == 0 || patterns == null|| patterns.length == 0)return -1;int index = -1;for (int tIndex = 0, tLen = targets.length, pLen = patterns.length; tIndex < tLen; tIndex++) {int tmp = tIndex;for (int pIndex = 0; pIndex < pLen && tmp < tLen&& targets[tmp++] == patterns[pIndex]; pIndex++) {if (pIndex == pLen - 1)index = tIndex;}if (index != -1)break;}return index;}public static Integer[] traditionals(char[] targets, char[] patterns) {if (targets == null || targets.length == 0 || patterns == null|| patterns.length == 0)return new Integer[0];List<Integer> indexList = new ArrayList<Integer>();for (int tIndex = 0, tLen = targets.length, pLen = patterns.length; tIndex < tLen; tIndex++) {int tmp = tIndex;for (int pIndex = 0; pIndex < pLen && tmp < tLen&& targets[tmp++] == patterns[pIndex]; pIndex++) {if (pIndex == pLen - 1)indexList.add(tIndex);}}return indexList.toArray(new Integer[indexList.size()]);}public static Integer[] kmps(char[] targets, char[] patterns) {if (targets == null || targets.length == 0 || patterns == null|| patterns.length == 0)return new Integer[0];List<Integer> indexList = new ArrayList<Integer>();int[] cPFs = cptPfFunc(patterns);int pIndex = 0;for (int tIndex = 0, tLen = targets.length, pLen = patterns.length; tIndex < tLen; tIndex++) {while (pIndex > 0 && patterns[pIndex] != targets[tIndex])pIndex = cPFs[pIndex - 1];if (patterns[pIndex] == targets[tIndex])pIndex++;if (pIndex == pLen) {indexList.add(tIndex - pLen + 1);pIndex = cPFs[pIndex - 1];}}return indexList.toArray(new Integer[indexList.size()]);}public static int kmp(char[] targets, char[] patterns) {if (targets == null || targets.length == 0 || patterns == null|| patterns.length == 0)return -1;int index = -1;int[] cPFs = cptPfFunc(patterns);int pIndex = 0;for (int tIndex = 0, tLen = targets.length, pLen = patterns.length; tIndex < tLen; tIndex++) {while (pIndex > 0 && patterns[pIndex] != targets[tIndex])pIndex = cPFs[pIndex - 1];if (patterns[pIndex] == targets[tIndex])pIndex++;if (pIndex == pLen)index = tIndex - pLen + 1;if (index != -1)break;}return index;}public static int[] cptPfFunc(char[] patterns) {int size = patterns.length;int[] cPFs = new int[size];cPFs[0] = 0;int cIndex = 0;for (int pIndex = 1; pIndex < size; pIndex++) {while (cIndex > 0 && patterns[cIndex] != patterns[pIndex])cIndex = cPFs[cIndex];if (patterns[cIndex] == patterns[pIndex])cIndex++;cPFs[pIndex] = cIndex;}return cPFs;}public static void main(String[] args) {char[] targets = "kkabcdefijklmnabcdefijkabcdlmn".toCharArray();char[] patterns = "abcd".toCharArray();out.println("-------------First Matching Index-----------------");out.println("traditional first match: "+ traditional(targets, patterns));out.println("kmp first match: " + kmp(targets, patterns));out.println("-------------Traditional Matching Array-----------------");Integer[] tIndexs = traditionals(targets, patterns);for (int i = 0, tLen = tIndexs.length; i < tLen; i++) {out.print(tIndexs[i] + " ");}out.println("\n-------------KMP Matching Array-----------------");Integer[] kmpIndexs = kmps(targets, patterns);for (int i = 0, kmpLen = kmpIndexs.length; i < kmpLen; i++) {out.print(kmpIndexs[i] + " ");}}}
测试结果:
-------------First Matching Index-----------------
traditional first match: 2
kmp first match: 2
-------------Traditional Matching Array-----------------
2 14 23
-------------KMP Matching Array-----------------
2 14 23
- 字符串匹配问题:KMP算法
- KMP算法解决字符串匹配问题
- KMP算法——字符串匹配问题
- 字符串匹配问题(KMP算法)
- KMP算法用于字符串匹配问题
- KMP算法解决字符串匹配问题
- KMP算法解决字符串匹配问题
- 字符串匹配问题——KMP算法
- KMP算法模板(字符串匹配问题)
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- Leetcode: Trapping Rain Water
- UVa 686 Goldbach's Conjecture (II) (哥德巴赫猜想)
- 设计模式之适配器模式
- C#中ADO.NET连接数据库中的DbNull 与 null的区别
- Github执行git add -A 出现 LF will be replaced by CRLF in *
- 字符串匹配问题:KMP算法
- 十道大数据的题
- c++有关编译期和运行期的访问
- JBoss 系列三:使用4种方式部署应用到JBoss7/WildFly
- 十七道海量数据面试题
- 使用GNU 编译OBjectC
- hibernate之HQL之参数绑定
- Ringo.js嵌入集成(二):使用Listener Filter替换官方的JsgiServlet实现
- OpenCL 和 CUDA 之间的区别