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 (texti + j != patternj)

{

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;
        }
    }
}
相关推荐
SWAGGY..20 分钟前
Linux系统编程:(十三)环境变量
java·linux·算法
Black蜡笔小新29 分钟前
自动化AI算法训练服务器DLTM一体化训推平台构建企业专属AI能力中台
人工智能·算法·自动化
sjsjs1143 分钟前
力扣3558. 给边赋权值的方案数 I
算法·leetcode·职场和发展
hujinyuan2016044 分钟前
2025年12月中国电子学会青少年机器人技术等级考试试卷(四级) 真题+答案
算法·机器人
啦啦啦啦啦zzzz1 小时前
算法总结(双指针)
c++·算法·双指针
花间相见1 小时前
【LeetCode01】—— 无重复字符的最长子串:滑动窗口经典题详解
python·算法·leetcode
wabs6661 小时前
关于动态规划【力扣96.不同的二叉搜索树的递推公式怎么理解?】
算法·动态规划
QiLinkOS1 小时前
极客与商业思维的融合实践(1)
c语言·数据库·c++·人工智能·算法·开源协议
fu的博客1 小时前
【数据结构16】图:基于邻接矩阵、邻接表实现DFS/BFS
数据结构·算法
阿正的梦工坊1 小时前
【Rust】17-Send、Sync 与并发安全抽象
算法·安全·rust