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;
        }
    }
}
相关推荐
明洞日记2 分钟前
【VTK手册034】 vtkGeometryFilter 深度解析:高性能几何提取与转换专家
c++·图像处理·算法·ai·vtk·图形渲染
额呃呃4 分钟前
operator new/delete
开发语言·c++·算法
hweiyu0018 分钟前
二分图匹配算法:匈牙利算法
算法
IAR Systems21 分钟前
在IAR Embedded Workbench for Renesas RH850中实现ROPI
linux·运维·算法
一个不知名程序员www36 分钟前
算法学习入门--- set与map(C++)
c++·算法
鸿途优学-UU教育44 分钟前
法考命题趋势解读:为何越来越重视“实战能力”?
算法·法律·uu教育·法考机构
Rui_Freely1 小时前
Vins-Fusion之 相机—IMU在线标定(两帧间旋转估计)(十)
人工智能·算法·计算机视觉
mudtools1 小时前
基于.NET操作Excel COM组件生成数据透视报表
c#·.net·excel
mit6.8241 小时前
二分猜答案
算法
_OP_CHEN1 小时前
【算法基础篇】(四十二)数论之欧拉函数深度精讲:从互质到数论应用
c++·算法·蓝桥杯·数论·欧拉函数·算法竞赛·acm/icpc