您的位置:首页 > 其它

字符串匹配问题:KMP算法

2013-10-05 11:59 351 查看
传统字符串匹配和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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: