sunday算法
来源:互联网 发布:怎样开通淘宝直播 编辑:程序博客网 时间:2024/04/27 23:42
原帖:http://kmplayer.iteye.com/blog/704187
1.Sunday算法是Daniel M.Sunday于1990年提出的一种比BM算法搜索速度更快的算法。
2.Sunday算法其实思想跟BM算法很相似,只不过Sunday算法是从前往后匹配,在匹配失败时关注的是文本串中参加匹配的最末位字符的下一位字符。如果该字符没有在匹配串中出现则直接跳过,即移动步长= 匹配串长度+ 1;否则,同BM算法一样其移动步长=匹配串中最右端的该字符到末尾的距离+1。
3.举例:
匹配串:abcbczdxzc
模式串:zbcac
这里我们看到z-a没有对上,我们就看匹配串中的z在模式串的位置,然后对齐。
匹配串:abcbczdxzc
模式串: zbcac
如果模式串中的没有那个字符的话就跳过去。
匹配串:abcbcedxzcs
模式串:zbcac
e不在模式串中出现,那么我们就
匹配串:abcbcedxzcs
模式串: zbcac
#include <iostream>#include <cstring>using namespace std;int sunday(const char* src, const char* des){int len_s = strlen(src);int len_d = strlen(des);int next[26] = {0};for (int j = 0; j < 26; ++j)next[j] = len_d + 1;for (int j = 0; j < len_d; ++j)next[des[j] - 'a'] = len_d - j; //记录字符到最右段的最短距离+1//例如:des = "abcedfb"//next = {7 1 5 4 3 2 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8}int pos = 0;while (pos < (len_s - len_d + 1)) //末端对齐{int i = pos;int j;for (j = 0; j < len_d; ++j, ++i){if (src[i] != des[j]){pos += next[src[pos + len_d] - 'a'];//不等于就跳跃,跳跃是核心break;}}if ( j == len_d )return pos;}return -1;}int main(){ char src[]="abcdacdaahfacabcdabcdeaa"; char des[]="abcde"; cout<<sunday(src,des)<<endl; return 0;}
0 0
- Sunday算法
- Sunday算法
- Sunday算法
- Sunday算法
- SUNDAY 算法
- Sunday算法
- sunday算法
- Sunday算法
- Sunday算法
- sunday 算法
- Sunday算法
- sunday算法
- sunday算法
- sunday算法
- sunday算法
- sunday算法
- Sunday 算法
- SUNDAY 算法 c#实现
- Linux 下 Xdebug 的安装
- Unity3D的JS和C#脚本互调
- 要提高SQL查询效率where语句条件的先后次序应如何写使你的SQL 语句完全优化
- 腾讯、优酷视频地址解析
- Android音频系统之AudioTrack(一)
- sunday算法
- 互联网推广
- intent 实现对象或者列表的传递
- js 操作cookies 步骤
- eclipse中php插件安装
- 初识云计算day014
- Redis源码学习——双端链表
- 实现:C#窗体中的文本框只能输入中文汉字,其他输入无效。问:正则表达式怎么用?
- <<编程之美>>之一摞烙饼的排序