C#,字符串匹配(模式搜索)原生(Native)算法的源代码

算法没什么可说的,就是一段一段匹配呗。

运行效果:

源代码:

using System;

using System.Collections;

using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm

{

/// <summary>

/// 字符串匹配(模式搜索)算法集锦

/// </summary>

public static partial class PatternSearch

{

/// <summary>

/// 字符串匹配的暴力算法(1)

/// </summary>

/// <param name="text"></param>

/// <param name="pattern"></param>

/// <returns></returns>

public static List<int> NativeSearch_Original(string text, string pattern)

{

int pt = pattern.Length;

List<int> matchs = new List<int>();

for (int i = 0; i < (text.Length - pt); i++)

{

if (text.Substring(i, pt) == pattern)

{

matchs.Add(i);

}

}

return matchs;

}

/// <summary>

/// 字符串匹配的暴力算法(2)

/// </summary>

/// <param name="text"></param>

/// <param name="pattern"></param>

/// <returns></returns>

public static List<int> Native_Search(string text, string pattern)

{

List<int> matchs = new List<int>();

int M = pattern.Length;

int N = text.Length;

int S = N - M;

if (S <= 0) return matchs;

for (int i = 0; i <= S; i++)

{

int j = 0;

while (j < M)

{

if (text[i + j] != pattern[j])

{

break;

}

j++;

}

if (j == M)

{

matchs.Add(i);

}

}

return matchs;

}

}

}

--------------================--------------------

POWER BY TRUFFER.CN

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// 字符串匹配(模式搜索)算法集锦
    /// </summary>
    public static partial class PatternSearch
    {
        /// <summary>
        /// 字符串匹配的暴力算法(1)
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static List<int> NativeSearch_Original(string text, string pattern)
        {
            int pt = pattern.Length;
            List<int> matchs = new List<int>();
            for (int i = 0; i < (text.Length - pt); i++)
            {
                if (text.Substring(i, pt) == pattern)
                {
                    matchs.Add(i);
                }
            }
            return matchs;
        }

        /// <summary>
        /// 字符串匹配的暴力算法(2)
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static List<int> Native_Search(string text, string pattern)
        {
            List<int> matchs = new List<int>();

            int M = pattern.Length;
            int N = text.Length;
            int S = N - M;

            if (S <= 0) return matchs;
            for (int i = 0; i <= S; i++)
            {
                int j = 0;
                while (j < M)
                {
                    if (text[i + j] != pattern[j])
                    {
                        break;
                    }
                    j++;
                }

                if (j == M)
                {
                    matchs.Add(i);
                }
            }

            return matchs;
        }
    }
}
相关推荐
future14128 分钟前
C#学习日记
开发语言·学习·c#
martian6651 小时前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
孟大本事要学习1 小时前
算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
算法
傻啦嘿哟1 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
??tobenewyorker2 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
贾全2 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天2 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
满分观察网友z3 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
森焱森4 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天4 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid