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

Sunday算法是Daniel M.Sunday于1990年提出的一种字符串模式匹配算法。

核心思想:在匹配过程中,模式串并不被要求一定要按从左向右进行比较还是从右向左进行比较,它在发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率。

Sunday算法思想跟BM(Boyer Moore)算法很相似,在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符。如果该字符没有在匹配串中出现则直接跳过,即:移动步长=匹配串长度+1;否则,同BM算法一样,其移动步长=匹配串中最右端的该字符到末尾的距离+1。

本代码运行效果:

源代码:

using System;

using System.Text;

using System.Collections;

using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm

{

public static partial class PatternSearch

{

/// <summary>

/// 字符位置表

/// </summary>

private static int ALPHA_BET = 512;

/// <summary>

/// 计算字符的出现位置表

/// </summary>

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

/// <returns></returns>

private static int[] ComputeOccurence(string pattern)

{

int[] table = new int[ALPHA_BET];

for (char a = (char)0; a < (char)ALPHA_BET; a++)

{

table[(int)a] = -1;

}

for (int i = 0; i < pattern.Length; i++)

{

char a = pattern[i];

table[(int)a] = i;

}

return table;

}

/// <summary>

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

/// </summary>

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

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

/// <returns></returns>

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

{

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

int i = 0;

int[] table = ComputeOccurence(pattern);

while (i <= text.Length - pattern.Length)

{

int j = 0;

while (j < pattern.Length && text[i + j] == pattern[j])

{

j++;

}

if (j == pattern.Length)

{

matchs.Add(i);

}

i += pattern.Length;

if (i < text.Length)

{

i -= table[(int)text[i]];

}

}

return matchs;

}

}

}


POWER BY 315SOFT.COM &
TRUFFER.CN

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

namespace Legalsoft.Truffer.Algorithm
{
    public static partial class PatternSearch
    {
        /// <summary>
        /// 字符位置表
        /// </summary>
        private static int ALPHA_BET = 512;

        /// <summary>
        /// 计算字符的出现位置表
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private static int[] ComputeOccurence(string pattern)
        {
            int[] table = new int[ALPHA_BET];
            for (char a = (char)0; a < (char)ALPHA_BET; a++)
            {
                table[(int)a] = -1;
            }

            for (int i = 0; i < pattern.Length; i++)
            {
                char a = pattern[i];
                table[(int)a] = i;
            }
            return table;
        }

        /// <summary>
        /// 字符串匹配算法(模式搜索)Sunday算法
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static List<int> Sunday_Search(string text, string pattern)
        {
            List<int> matchs = new List<int>();

            int i = 0;
            int[] table = ComputeOccurence(pattern);
            while (i <= text.Length - pattern.Length)
            {
                int j = 0;
                while (j < pattern.Length && text[i + j] == pattern[j])
                {
                    j++;
                }
                if (j == pattern.Length)
                {
                    matchs.Add(i);
                }
                i += pattern.Length;
                if (i < text.Length)
                {
                    i -= table[(int)text[i]];
                }
            }
            return matchs;
        }
    }
}
相关推荐
CoovallyAIHub6 分钟前
单目深度估计重大突破:无需标签,精度超越 SOTA!西湖大学团队提出多教师蒸馏新方案
深度学习·算法·计算机视觉
CoovallyAIHub9 分钟前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
深度学习·算法·计算机视觉
偷偷的卷37 分钟前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
北京地铁1号线43 分钟前
Zero-Shot(零样本学习),One-Shot(单样本学习),Few-Shot(少样本学习)概述
人工智能·算法·大模型
凤年徐1 小时前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
kualcal1 小时前
代码随想录17|二叉树的层序遍历|翻转二叉树|对称二叉树
数据结构·算法
满分观察网友z2 小时前
从混乱到有序:我用“逐层扫描”法优雅搞定公司组织架构图(515. 在每个树行中找最大值)
后端·算法
满分观察网友z2 小时前
一行代码的惊人魔力:从小白到大神,我用递归思想解决了TB级数据难题(3304. 找出第 K 个字符 I)
后端·算法
字节卷动2 小时前
【牛客刷题】活动安排
java·算法·牛客
yi.Ist3 小时前
数据结构 —— 键值对 map
数据结构·算法