惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 如斯夫

Windows Azure中的Affinity Group .NET内存管理 .NET程序的运行与内存管理 堆栈和堆 应用程序的装载与运行 该怎么教育 COM学习 完成了吗? 技术人员面试流程泳道图 创建型模式 C# 面试题目 单链表中删除重复数据 C# 数据结构 单链表反转 C# 面试算法 人的问题 foreach中的隐式类型转换 C# 点滴 - 枚举 数据库惊魂 没有人能随随便便成功 If you are a new test manager – From google testing blog
字符串匹配算法 – Sunday算法
如斯夫 · 2012-05-25 · via 博客园 - 如斯夫

假设我们有如下字符串:

A = "LESSONS TEARNED IN SOFTWARE TE";

B = "SOFTWARE";

Sunday算法的大致原理是:

先从左到右逐个字符比较,以我们的字符串为例:

开始的时候,我们让i = 0, 指向A的第一个字符; j = 0 指向B的第一个字符,分别为"L"和"S",不等;这个时候,Sunday算法要求,找到位于A字串中位于B字符串后面的第一个字符,即下图中 m所指向的字符"T",在模式字符串B中从后向前查找是否存在"T",

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

i 

       

                     

S 

                      

j 

                             

可以看到下图中k指向的字符与m指向的字符相等,

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

i 

       

                     

S 

                      

j 

  

                          

这时就将相等的字符对齐,让j再次指向B字符串的头一个字符,相应地,将i指向主串对应的字符N

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

     

  

                          

                      

  

                     

再次比较A[i]和B[j],不等,这时再次寻找主串中在模式串后面的那个字符

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

     

       

                     

                      

      

                 

我们看到,模式串的最后一个字符与m指向的主串字符相等,因此再次移动子串

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

      

      

                      

                      

                       

这时,主串i对应的字符是S,j对应的子串字符也是S,i++, j++

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

       

     

                      

                       

                      

现在再次不等,m指向字符"D"

L 

E 

S 

S 

O 

N 

S 

 

T 

E 

A 

R 

N 

E 

D 

 

I 

N 

 

S 

O 

F 

T 

W 

A 

R 

E 

 

T 

E 

       

      

                     

                       

                      

….

直到找到,或者i到达主串的末尾

C#代码如下:

static int SundaySearch(string text, string pattern)

        {

            int i = 0;

            int j = 0;

            int pe = pattern.Length - 1;

            int tb = i;

            int te = text.Length - 1;

            while (i < text.Length && j < pattern.Length)

            {

                if (text[i] == pattern[j])

                {

                    i++;

                    j++;

                }

                else

                {

                    int k = pattern.Length - 1;

                    while (k >= 0 && text[pe + 1] != pattern[k])

                    {

                        k--;

                    }

                    int gap = pattern.Length - k;

                    i += gap;

                    pe = i + pattern.Length - 1;

                    tb = i;

                    j = 0;

                }

            }

            if (i <= text.Length)

            {

                return tb;

            }

            return -1;

        }