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;
        }
    }
}
相关推荐
Miraitowa_cheems5 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号5 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区6 小时前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
fengfuyao9857 小时前
BCH码编译码仿真与误码率性能分析
算法
小白不想白a7 小时前
每日手撕算法--哈希映射/链表存储数求和
数据结构·算法
剪一朵云爱着8 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
落日漫游8 小时前
数据结构笔试核心考点
java·开发语言·算法
workflower8 小时前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
仰泳的熊猫8 小时前
LeetCode:701. 二叉搜索树中的插入操作
数据结构·c++·算法·leetcode
kali-Myon8 小时前
NewStarCTF2025-Week2-Pwn
算法·安全·gdb·pwn·ctf·栈溢出