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;
        }
相关推荐
仟濹11 分钟前
【算法打卡day20(2026-03-12 周四)算法/技巧:哈希表,双指针,字符串交换处理】5个题
数据结构·算法·散列表
陌夏12 分钟前
双指针与滑动窗口
算法
小曹要微笑16 分钟前
WinForms 验证码类的实现
c#·验证码·winform·验证码类
MicroTech202521 分钟前
MLGO微算法科技,推出革命性量子算法ANQITE,推动量子计算新时代
科技·算法·量子计算
样例过了就是过了42 分钟前
LeetCode热题100 子集
数据结构·c++·算法·leetcode·dfs
I_LPL1 小时前
day52 代码随想录算法训练营 图论专题5
java·算法·图论·并查集
jing-ya1 小时前
day 49 图论part1
算法·深度优先·图论
想吃火锅10051 小时前
【leetcode】98.验证二叉搜索树
算法·leetcode·职场和发展
武藤一雄1 小时前
告别繁琐的 out 参数:C# 现代元组(ValueTuple)如何重构你的方法返回值
microsoft·c#·asp.net·.net·.netcore
一叶落4381 小时前
【LeetCode 172】阶乘后的零(C语言详解 | 数学规律 + 对数时间复杂度)
c语言·数据结构·算法·leetcode·动态规划