字符串匹配问题:KMP算法
2013-10-05 11:59
351 查看
传统字符串匹配和KMP算法
转载时请注明来源:http://blog.csdn.net/ccfeng2008
JAVA源码如下:
测试结果:
-------------First Matching Index-----------------
traditional first match: 2
kmp first match: 2
-------------Traditional Matching Array-----------------
2 14 23
-------------KMP Matching Array-----------------
2 14 23
转载时请注明来源: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
相关文章推荐
- 十道大数据的题
- Tomcat原理详解和各种集群的实现
- 扩展Activiti流程定义文件,实现自定义节点属性
- 验房注意事项
- C#中ADO.NET连接数据库中的DbNull 与 null的区别
- Github执行git add -A 出现 LF will be replaced by CRLF in *
- 第三章6
- 约瑟夫环问题
- 细说C#中连接字符串的方法“+”和Append
- UVa 686 Goldbach's Conjecture (II) (哥德巴赫猜想)
- java中的匿名内部类总结
- 3-10
- (转)什么是生成函数?
- alter index rebuild 与 rebuild online
- MongoDB学习笔记2--MongoDB数据库中数据的导入
- java中小数的四舍六入和四舍五入
- VC中类型的相互转化
- 【§伪win7壁纸xp主题(oo)】
- 【§伪win7壁纸xp主题(oo)】
- Winform001 Winform中在一定区域内移动控件