Sunday algorithm

本文介绍了一种快速子字符串搜索算法——周日算法。该算法在Boyer-Moore和Horspool算法的基础上进行了改进,利用文本窗口右侧字符进行模式匹配,从而实现更高效的搜索过程。文章还详细解释了其预处理步骤及搜索算法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Idea

http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm

The Boyer-Moore-algorithm uses for its bad-character heuristics the text symbol that has caused a mismatch. The Horspool-algorithmuses the rightmost symbol of the current text window. It was observed by Sunday [Sun 90] that it may be even better to use the symbol directly right of the text window, since in any case this symbol is involved in the next possible match of the pattern.

Example:  

0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
 bcaab     
 
0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
    bcaab  
 
0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
      bcaab
     
(a)   Boyer-Moore (b)   Horspool (c)   Sunday
     

In this example, t0, ..., t4 =  a b c a b is the current text window that is compared with the pattern. Its suffix a b has matched, but the comparison c-a causes a mismatch. The bad-character heuristics of the Boyer-Moore algorithm (a) uses the "bad" text character c to determine the shift distance. The Horspool algorithm (b) uses the rightmost character b of the current text window. The Sunday algorithm (c) uses the character directly right of the text window, namely d in this example. Since d does not occur in the pattern at all, the pattern can be shifted past this position.

 

Like the Boyer-Moore and the Horspool algorithm, the Sunday algorithm assumes its best case if every time in the first comparison a text symbol is found that does not occur at all in the pattern. Then the algorithm performs just O(n/m) comparisons.

In contrast to the Boyer-Moore and the Horspool algorithm the pattern symbols need not be compared from right to left. They can be compared in an arbitrary order. For instance, this order can depend on the symbol probabilities, provided they are known. Then the least probable symbol in the pattern is compared first, hoping that it does not match, so that the pattern can be shifted

The following example shows the comparisons performed if symbol c of the pattern is compared first..

Example:  

0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
      bcaab

 

Preprocessing

The occurrence function occ required for the bad-character heuristics is computed in the same way as in the Boyer-Moore algorithm.

Given a pattern p, the following function sundayInitocc computes the occurrence function; it is identical to the function bmInitocc.

void sundayInitocc()
{
    int j;
    char a;

    for (a=0; a<alphabetsize; a++)
        occ[a]=-1;

    for (j=0; j<m; j++)
    {
        a=p[j];
        occ[a]=j;
    }
}



Searching algorithm
 

Using a function matchesAt that compares the pattern with the text window in a certain manner depending on the implementation, the searching algorithm looks as follows:


void sundaySearch()
{
    int i=0;
    while (i<=n-m)
    {
        if (matchesAt(i)) report(i);
        i+=m;
        if (i<n) i-=occ[t[i]];
    }
}


 After statement i+=m, it is necessary to check if the value of i is at most n-1, since subsequently t[i] is accessed.

References

   
[Sun 90] D.M. Sunday: A Very Fast Substring Search Algorithm. Communications of the ACM, 33, 8, 132-142 (1990)
  
[1]http://www-igm.univ-mlv.fr/~lecroq/string/  

 

标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
SUNDAY算法,全称为“Sunday’s Algorithm”,是一种用于计算某个日期是一年中的第几天的算法。这个算法是由英国数学家John Horton Conway提出的。在C语言中,你可以按照以下步骤来实现: ```c #include <stdio.h> #include <math.h> int sundaysAlgorithm(int day, int month, int year) { int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 判断是否闰年 int isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if (isLeapYear) { daysInMonth[2] = 29; // 闰年的二月有29天 } int totalDays = day; for (int i = 1; i < month; i++) { totalDays += daysInMonth[i]; } // 加上前一年的天数(从当年1月1日到前一年12月31日) if (month > 1) { totalDays -= daysInMonth[month - 1]; // 减去当前月份的第一天,因为我们已经加上了它的日子 } // 如果是闰年且2月最后一天是星期一,则需要加1(因为闰年的2月29日是星期一) if (isLeapYear && ((day == 1) && (month == 2))) { totalDays++; } return (totalDays + year - 1) % 7; // 根据模7的结果确定星期几,结果范围0-6,其中0代表周日 } int main() { int inputDay, inputMonth, inputYear; printf("请输入日期(格式:DD MM YYYY): "); scanf("%d %d %d", &inputDay, &inputMonth, &inputYear); int result = sundaysAlgorithm(inputDay, inputMonth, inputYear); switch (result) { case 0: printf("这一天是星期日.\n"); break; case 1: printf("这一天是星期一.\n"); break; case 2: printf("这一天是星期二.\n"); break; case 3: printf("这一天是星期三.\n"); break; case 4: printf("这一天是星期四.\n"); break; case 5: printf("这一天是星期五.\n"); break; case 6: printf("这一天是星期六.\n"); break; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值