字符串匹配问题:KMP算法

18 篇文章 0 订阅
15 篇文章 0 订阅

传统字符串匹配和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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值