求高手解释一个字符串匹配算法 Shift-and算法

paglezjq 2008-05-22 07:37:03
shift-and算法是一种基于前缀搜索的字符串匹配算法,同出于KMP算法系列。我主要难理解的部分是位掩码的C++实现,谁能帮我解释下这个地方啊,最好能给出shitf-and算法的伪代码
...全文
245 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
WingForce 2008-05-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lin_style 的回复:]
只了解KMP的飘过。。
100分很诱人。。
[/Quote]
xkyx_cn 2008-05-22
  • 打赏
  • 举报
回复
这里有一个c++实现的例子


// StringMatch.hpp
#ifndef STRING_MATCH_HPP
#define STRING_MATCH_HPP

#include <boost/dynamic_bitset.hpp>
#include <iostream>

class Matcher
{
        struct DummyAction
        {
                void operator()(const std::string& s, size_t pos, size_t length) const
                {
                }
        };
public:
        class State
        {
                friend class Matcher;
        private:
                size_t pos;
                boost::dynamic_bitset<> D;
        };

        Matcher(const std::string& pattern)
        {
                pattern_length = pattern.size();
                for (int i = 0; i < UCHAR_MAX; ++i)
                        B[i].resize(pattern_length);

                for (size_t i = 0; i < pattern_length; ++i)
                        B[(unsigned char)pattern[i]].set(i);
        }

        template <typename MatchAction>
        int Match(const std::string& s, MatchAction action)
        {
                int result = 0;
                boost::dynamic_bitset<> D(pattern_length);
                for (size_t i = 0; i<s.length(); ++i)
                {
                        D <<= 1;
                        D.set(0);
                        D &= B[(unsigned char)s[i]];
                        if (D.test(pattern_length - 1))
                        {
                                action(s, i - pattern_length + 1, pattern_length);
                                ++result;
                        }
                }

                return result;
        }

        int Match(const std::string& s)
        {
                DummyAction action;
                return Match(s, action);
        }

        int FirstMatch(const std::string& s, State& state)
        {
                state.D.resize(pattern_length);
                state.pos = 0;
                return NextMatch(s, state);
        }

        int NextMatch(const std::string& s, State& state)
        {
                for (; state.pos < s.length(); ++state.pos)
                {
                        state.D <<= 1;
                        state.D.set(0);
                        state.D &= B[(unsigned char)s[state.pos]];
                        if (state.D.test(pattern_length - 1))
                        {
                                ++state.pos;
                                return (int)(state.pos - pattern_length);
                        }
                }
                return -1;
        }
private:
        boost::dynamic_bitset<> B[UCHAR_MAX + 1];
        size_t pattern_length;
};

#endif//STRING_MATCH_HPP



// test.cpp
#include "StringMatch.hpp"

void ReportMatch(const std::string& s, size_t pos, size_t length)
{
        std::cout << s.substr(pos, length) << '\n';
}

int main()
{
        const std::string pattern("announce"); 
        const std::string s("announceannounceannounceannounceannounce"); 
        Matcher m(pattern);
        m.Match(s, ReportMatch);
        Matcher::State state;
        for (int n = m.FirstMatch(s, state); n >= 0; n = m.NextMatch(s, state))
                std::cout << s.substr(n, pattern.length()) << '\n';

}

laolaoliu2002 2008-05-22
  • 打赏
  • 举报
回复
http://www.cnblogs.com/CampFeather/articles/536313.html
laolaoliu2002 2008-05-22
  • 打赏
  • 举报
回复
http://www.chinaitpower.com/A/2003-01-04/45995.html
hslinux 2008-05-22
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_3dc2673e01008mx6.html

http://www.juyudao.cn/bbs/viewthread.php?tid=481
薛定谔之死猫 2008-05-22
  • 打赏
  • 举报
回复
没研究这个,帮顶
lin_style 2008-05-22
  • 打赏
  • 举报
回复
只了解KMP的飘过。。
100分很诱人。。
paglezjq 2008-05-22
  • 打赏
  • 举报
回复
#include <boost/dynamic_bitset.hpp>

这个库看不懂,

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧