zoukankan      html  css  js  c++  java
  • ACM -- 算法小结(五)字符串算法之Sunday算法

    1. Sunday算法是Daniel M.Sunday1990年提出的一种比BM算法搜索速度更快的算法。 

    2. Sunday算法其实思想跟BM算法很相似,只不过Sunday算法是从前往后匹配,

       在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符。

       如果该字符没有在匹配串中出现则直接跳过,即移动步长匹配串长度+ 1

       否则,同BM算法一样其移动步长=匹配串中最右端的该字符到末尾的距离+1。 

     3. 举例如下:

    //pos=0;
    //匹配串:abcdacdaahfacabcdabcdeaa
    //模式串:abcde 
    //这里我们看到a-e没有对上,我们就看匹配串中的pos+len2在模式串的位置,然后对齐。 
    
    //匹配串:abcdacdaahfacabcdabcdeaa
    //模式串:   abcde 
    //pos=3; 
    //这里我们看到d-a没有对上,我们就看匹配串中的pos+len2在模式串的位置,然后对齐。 
    
    //匹配串:abcdacdaahfacabcdabcdeaa
    //模式串:        abcde 
    //pos=8; 
    //这里我们看到h-b没有对上,我们就看匹配串中的pos+len2在模式串的位置,然后对齐。 
    
    //匹配串:abcdacdaahfacabcdabcdeaa
    //模式串:           abcde 
    //pos=13; 
    //这里我们看到c-b没有对上,我们就看匹配串中的pos+len2在模式串的位置,然后对齐。 
    
    //匹配串:abcdacdaahfacabcdabcdeaa
    //模式串:                 abcde 
    //pos=17; 
    //这里我们看到模式串完全匹配

     

    代码演示如下:

     1 #include <iostream>   
     2 #include <cstring>   
     3 using namespace std;   
     4 
     5 char T[10001];
     6 char P[26];
     7 int next[26];
     8 
     9 int sunday(const char* T, const char* P)   
    10 {   
    11     int len1=strlen(T);   
    12     int len2=strlen(P);   
    13     memset(next,0,sizeof(next)); 
    14     
    15     for(int j=0; j<26;++j)   
    16         next[j]=len2+1;   
    17     for(j=0; j<len2;++j) 
    18     {
    19         next[P[j]-'a']=len2-j; //记录字符到最右段的最短距离+1
    20         //cout<<"next["<<P[j]-'a'<<"]="<<next[P[j]-'a']<<endl;
    21     }    
    22     //例如:p[]="abcedfb"   
    23     //next = {7 6 5 4 3 2 1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8}
    24     
    25     int pos = 0;   
    26     while(pos<(len1-len2+1)) //末端对齐   
    27     {   
    28         int i=pos;   
    29         int j;   
    30         for(j=0;j<len2;++j,++i)   
    31         {   
    32             if(T[i]!=P[j])  //不等于就跳跃,跳跃是核心  
    33             {   
    34                 pos+= next[T[pos + len2]-'a'];  
    35                 //cout<<"pos="<<pos<<endl<<endl;
    36                 break;   
    37             }   
    38         }   
    39         if(j==len2)   
    40             return pos;   
    41     }   
    42     return -1;   
    43 }    
    44 int main()   
    45 {    
    46     char T[]="abcdacdaahfacabcdabcdeaa";
    47     char P[]="abcde"; 
    48     while(scanf("%s%s",T,P)!=EOF)
    49         cout<<sunday(T,P)<<endl;   
    50     return 0;   
    51 }

     

  • 相关阅读:
    ERP渠道管理添加验证和查询(二十二)
    SqlServer导入Excel数据
    WebApi帮助类
    SqlServer 递归查询树
    SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句
    Excel上传找到错误数据类型
    索引Hint提示(INDEX Hint)
    SqlServer批量Sql一个表的数据导入到另一个数据
    SqlServer 游标
    JAVA运算符
  • 原文地址:https://www.cnblogs.com/lmei/p/3340681.html
Copyright © 2011-2022 走看看