C#实现KMP算法,在长字符串中找到第一个符合要求的子字符串

KMP算法可以查找符合要求的字符串的索引。

在下面的代码中会打印出方法所消耗的时间。

一些异常的处理:

  1. 比如当word比longwrod还要长的时候异常处理
  2. longwrod或者word为空的时候异常处理
  3. 当word在longwrod中不存在的时候异常处理
csharp 复制代码
       static void Main(string[] args)
        {
            string longwrod = "112312er3338bluifaehilf";
            string word = "3338";
            DateTime beforDT = System.DateTime.Now;
            TMPFunc(longwrod, word);
            DateTime afterDT = System.DateTime.Now;
            TimeSpan ts = afterDT.Subtract(beforDT);
            Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);
		
        }

        /// <summary>
        /// TMP算法_查找符合要求的字符串的索引
        /// </summary>
        /// <param name="longWord"> 长串</param>
        /// <param name="word">字串</param>
        public static void TMPFunc(string longWord, string word)
        {
            if (string.IsNullOrEmpty(longWord) || string.IsNullOrEmpty(word))
            {
                Console.WriteLine("长字符串和查找字符串不能为空。");
                return;
            }

            if (word.Length > longWord.Length)
            {
                Console.WriteLine("查找字符串长度大于长字符串长度,无法匹配。");
                return;
            }

            int[] prefixTable = CalculatePrefixTable(word);
            int longWordIndex = 0;
            int wordIndex = 0;

            while (longWordIndex < longWord.Length)
            {
                if (longWord[longWordIndex] == word[wordIndex])
                {
                    longWordIndex++;
                    wordIndex++;

                    if (wordIndex == word.Length)
                    {
                        // 找到了匹配
                        Console.WriteLine("匹配找到在索引 " + (longWordIndex - wordIndex));
                        wordIndex = prefixTable[wordIndex - 1];
                    }
                }
                else
                {
                    if (wordIndex != 0)
                    {
                        wordIndex = prefixTable[wordIndex - 1];
                    }
                    else
                    {
                        longWordIndex++;
                    }
                }
            }

            if (wordIndex == 0)
            {
                Console.WriteLine("未找到匹配。");
            }
        }

        private static int[] CalculatePrefixTable(string word)
        {
            int[] prefixTable = new int[word.Length];
            int prefixLength = 0;
            int i = 1;

            while (i < word.Length)
            {
                if (word[i] == word[prefixLength])
                {
                    prefixLength++;
                    prefixTable[i] = prefixLength;
                    i++;
                }
                else
                {
                    if (prefixLength != 0)
                    {
                        prefixLength = prefixTable[prefixLength - 1];
                    }
                    else
                    {
                        prefixTable[i] = 0;
                        i++;
                    }
                }
            }

            return prefixTable;
        }
相关推荐
昏睡红猹23 分钟前
C#脚本化(Roslyn):如何在运行时引入nuget包
c#
张人玉1 小时前
C# 常量与变量
java·算法·c#
weixin_446122462 小时前
LinkedList剖析
算法
就是有点傻2 小时前
在C#中,可以不实例化一个类而直接调用其静态字段
c#
软件黑马王子2 小时前
C#系统学习第八章——字符串
开发语言·学习·c#
阿蒙Amon2 小时前
C#读写文件:多种方式详解
开发语言·数据库·c#
百年孤独_3 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
就是有点傻3 小时前
C#如何实现中英文快速切换
数据库·c#
我爱C编程3 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
算法_小学生3 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展