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;
        }
    }
}
相关推荐
森焱森21 分钟前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack2 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客3 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠3 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
hie988944 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
杰克尼4 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
.30-06Springfield5 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦5 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
xiaolang_8616_wjl5 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
small_wh1te_coder5 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c