0
点赞
收藏
分享

微信扫一扫

C#,模式搜索KMP算法的源代码与数据可视化

转角一扇门 2022-04-07 阅读 57

模式搜索有很多算法。

一、KMP算法概述

KMP 算法(Knuth-Morris-Pratt 算法)是其中一个著名的、传统的字符串匹配算法,效率比较高。

KMP算法由D.E.KnuthJ.H.MorrisV.R.PrattBrute-Force算法的基础上提出的模式匹配的改进算法。因此人们称它为“克努特—莫里斯—普拉特算法”,简称KMP算法。KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。Brute- Force算法在模式串中有多个字符和主串中的若干个连续字符比较都相等,但最后一个字符比较不相等时,主串的比较位置需要回退。KMP算法在上述情况下,主串位置不需要回退,从而可以大大提高效率。

要点:实现方法主要是通过一个LPS(Longest Proper Suffix)数组实现,数组本身包含了模式串的局部匹配信息。

KMP算法的时间复杂度为O(m+n) 。

有些人以为讲清楚了,其实没有。

学习算法,阅读文字浪费时间,看图及阅读代码最好。

二、运行效果

本文源代码的运行效果:

三、核心代码

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Algorithm
{
	/// <summary>
	/// 模式搜索的 KMP 算法
	/// </summary>
	public static class Pattern_Search_KMP
	{
		/// <summary>
		/// 搜索
		/// </summary>
		/// <param name="pat"></param>
		/// <param name="txt"></param>
		public static List<int> Search(string pat, string txt)
		{
			List<int> result = new List<int>();

			int M = pat.Length;
			int N = txt.Length;

			int[] lps = new int[M];
			int j = 0;

			Build_LPS_Array(pat, M, lps);

			int i = 0;
			while (i < N)
			{
				if (pat[j] == txt[i])
				{
					j++;
					i++;
				}
				if (j == M)
				{
					result.Add(i - j);
					j = lps[j - 1];
				}

				else if (i < N && pat[j] != txt[i])
				{
					if (j != 0)
					{
						j = lps[j - 1];
					}
					else
					{
						i = i + 1;
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 构造 LPS 数组
		/// 最长后缀数组,Longest Proper Suffix 
		/// </summary>
		/// <param name="pat"></param>
		/// <param name="M"></param>
		/// <param name="lps"></param>
		private static void Build_LPS_Array(string pat, int M, int[] lps)
		{
			lps[0] = 0;
			int len = 0;
			int i = 1;
			while (i < M)
			{
				if (pat[i] == pat[len])
				{
					len++;
					lps[i] = len;
					i++;
				}
				else
				{
					if (len != 0)
					{
						len = lps[len - 1];
					}
					else
					{
						lps[i] = len;
						i++;
					}
				}
			}
		}
	}
}

四、改进KMP算法

软件的改进无非是用存储计算

保存更多的相邻信息,就可以提高计算速度。

举报

相关推荐

0 条评论