-
bm算法
-
-
-
-
-
-
-
-
-
-
- void BuildBadCharacterShift(char const* pattern, int plen, int* shift)
- {
- for( int i = 0; i < 256; i++ )
- *(shift+i) = plen;
- while ( plen >0 )
- {
- *(shift+(unsigned char)*pattern++) = --plen;
- }
- }
-
-
- bref 查找出模式串patn在主串src中第一次出现的位置
-
- */return patn在src中出现的位置,当src中并没有patn时,返回-1
-
- */
- int search_badcharacter(char const* src, int slen, char const* patn, int plen, int* shift)
- {
- int s_idx = plen, p_idx;
- int skip_stride;
- if (plen == 0)
- return -1;
- while (s_idx <= slen)
- {
- p_idx = plen;
- while (src[--s_idx] == patn[--p_idx])
- {
-
-
- if (p_idx == 0)
- {
- return s_idx;
- }
- }
- skip_stride = shift[(unsigned char)src[s_idx]];
- s_idx += (skip_stride>plen-p_idx ? skip_stride: plen-p_idx)+1;
- }
- return -1;
- }
-
-
-
-
-
-
-
-
-
- void BuildGoodSuffixShift(char const* pattern, int plen, int* shift)
- {
- shift[plen-1] = 1;
- char end_val = pattern[plen-1];
- char const* p_prev, const* p_next, const* p_temp;
- char const* p_now = pattern + plen - 2;
- bool isgoodsuffixfind = false;
- for( int i = plen -2; i >=0; --i, --p_now)
- {
- p_temp = pattern + plen -1;
- isgoodsuffixfind = false;
- while ( true )
- {
- while (p_temp >= pattern && *p_temp-- != end_val);
- p_prev = p_temp;
- p_next = pattern + plen -2;
-
-
-
-
-
-
- if( p_prev < pattern && *(p_temp+1) != end_val )
- break;
-
- bool match_flag = true;
- while( p_prev >= pattern && p_next > p_now )
- {
- if( *p_prev --!= *p_next-- )
- {
- match_flag = false;
- break;
- }
- }
-
- if( !match_flag )
- continue;
- else
- {
-
- if( p_prev < pattern || *p_prev != *p_next)
- {
-
- isgoodsuffixfind = true;
- break;
- }
-
- }
- }
- shift[i] = plen - i + p_next - p_prev;
- if( isgoodsuffixfind )
- shift[i]--;
- }
- }
-
-
- bref 查找出模式串patn在主串src中第一次出现的位置
-
- */return patn在src中出现的位置,当src中并没有patn时,返回-1
-
- */
- int search_goodsuffix(char const* src, int slen, char const* patn, int plen, int* shift)
- {
- int s_idx = plen, p_idx;
- int skip_stride;
- if (plen == 0)
- return -1;
-
- while (s_idx <= slen)
- {
- p_idx = plen;
- while (src[--s_idx] == patn[--p_idx])
- {
-
-
- if (p_idx == 0)
- {
- return s_idx;
- }
- }
- skip_stride = shift[p_idx];
- s_idx += skip_stride +1;
- }
- return -1;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- int BMSearch(char const*src, int slen, char const*ptrn, int plen, int const*bad_shift, int const*good_shift)
- {
- int s_idx = plen;
- if (plen == 0)
- return 1;
-
- while (s_idx <= slen)
- {
- int p_idx = plen, bad_stride, good_stride;
- while (src[--s_idx] == ptrn[--p_idx])
- {
-
-
-
- if (p_idx == 0)
- {
- return s_idx;
- }
- }
-
-
- bad_stride = bad_shift[(unsigned char)src[s_idx]];
- good_stride = good_shift[p_idx];
- s_idx += ((good_stride > bad_stride) ? good_stride : bad_stride )+1;
- }
- return -1;
- }
-
相关阅读:
wxpython模块学习
Linux Kernel TCP/IP Stack|Linux网络硬核系列
解 Linux 的 epoll 机制
取消 “安装未知发布者程序” 提示
添加 “开机自启动” 程序
x和y轴取对数之后的散点图
python 混淆矩阵可视化
fileserverMinio(进行中)
谷粒商城 分布式高级篇全文检索ElasticSearch进阶term查询 match查询的区别
谷粒学院高级118、全文检索ElasticSearch进阶aggregations聚合分析
-
原文地址:https://www.cnblogs.com/maifengqiang/p/3108285.html
Copyright © 2020-2023
润新知