字符串匹配問題:KMP算法

傳統字符串匹配和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


發佈了24 篇原創文章 · 獲贊 17 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章