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;
        }
    }
}
相关推荐
mit6.82416 分钟前
bfs|栈
算法
CoderYanger1 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz2 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
稚辉君.MCA_P8_Java2 小时前
DeepSeek 插入排序
linux·后端·算法·架构·排序算法
多多*2 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
.YM.Z3 小时前
【数据结构】:排序(一)
数据结构·算法·排序算法
Chat_zhanggong3453 小时前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法
百***48073 小时前
【Golang】slice切片
开发语言·算法·golang
墨染点香3 小时前
LeetCode 刷题【172. 阶乘后的零】
算法·leetcode·职场和发展
做怪小疯子3 小时前
LeetCode 热题 100——链表——反转链表
算法·leetcode·链表