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;
        }
相关推荐
森焱森1 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack3 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客3 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠4 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988944 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼4 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield5 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦5 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl5 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
small_wh1te_coder5 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c