KMP算法实现bit搜索
来源:互联网 发布:linux wifi 破解 编辑:程序博客网 时间:2024/05/21 06:51
工作需要,自己封装了一个比特搜索类,都是静态函数。用KMP算法实现。
这是头文件:
// BitSearch.h
#ifndef __CBITSEARCH__H
#define __CBITSEARCH__H
class CBitSearch
{
public:
static int IndexOfKMP( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
static int IndexOf( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
static int GetCount( const unsigned char *S, const unsigned char* T, int slen, int tlen, int start = 0 );
private:
static const unsigned char _fillmasks[];
static void GetNext( const unsigned char* T, int len, int next[] );
static inline bool GetBit( const unsigned char* T, int pos )
{
return ( T[pos>>3] & _fillmasks[pos%8] )?true:false;
}
};
#endif
#ifndef __CBITSEARCH__H
#define __CBITSEARCH__H
class CBitSearch
{
public:
static int IndexOfKMP( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
static int IndexOf( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start = 0 );
static int GetCount( const unsigned char *S, const unsigned char* T, int slen, int tlen, int start = 0 );
private:
static const unsigned char _fillmasks[];
static void GetNext( const unsigned char* T, int len, int next[] );
static inline bool GetBit( const unsigned char* T, int pos )
{
return ( T[pos>>3] & _fillmasks[pos%8] )?true:false;
}
};
#endif
这是实现文件:
// BitSearch.cpp
#include "BitSearch.h"
const unsigned char CBitSearch::_fillmasks[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
void CBitSearch::GetNext( const unsigned char* T, int len, int next[] )
{
int j = 0, k = -1;
next[0] = -1;
while( j < len )
{
if( k == -1 || GetBit(T,j) == GetBit(T,k) )
{
++j; ++k;
if( GetBit(T,j) != GetBit(T,k) ) next[j] = k;
else next[j] = next[k];
}
else k = next[k];
}
}
// Search the sub bit stream using KMP
// Caller should ensure the input parameters are legal
int CBitSearch::IndexOfKMP( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start )
{
if( start+tlen > slen ) return -1;
int *next = new int[tlen+1];
if(!next) return -1;
// Get the next value
GetNext(T, tlen, next);
int i = start, j = 0;
while( i < slen && j < tlen )
{
if(j == -1 || GetBit(S,i) == GetBit(T,j)) { ++i; ++j; }
else j = next[j];
}
delete[] next;
if( j == tlen ) return i-j;
else return -1;
}
// Caller should ensure the input parameters are legal
int CBitSearch::IndexOf( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start )
{
if( start+tlen > slen ) return -1;
int i = start, j = 0;
while( i < slen && j < tlen )
{
if(GetBit(S,i) == GetBit(T,j)) { ++i; ++j; }
else { i = i - j + 1; j = 0; }
}
if( j == tlen ) return i-j;
else return -1;
}
// Get the times of sub bit stream appears in source bit stream
int CBitSearch::GetCount( const unsigned char *S, const unsigned char* T, int slen, int tlen, int start )
{
int count = 0;
int pos = 0;
pos = IndexOfKMP( S, T, slen, tlen, start );
while( pos >= 0 )
{
pos = IndexOfKMP( S, T, slen, tlen, pos+tlen );
++count;
}
return count;
}
#include "BitSearch.h"
const unsigned char CBitSearch::_fillmasks[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
void CBitSearch::GetNext( const unsigned char* T, int len, int next[] )
{
int j = 0, k = -1;
next[0] = -1;
while( j < len )
{
if( k == -1 || GetBit(T,j) == GetBit(T,k) )
{
++j; ++k;
if( GetBit(T,j) != GetBit(T,k) ) next[j] = k;
else next[j] = next[k];
}
else k = next[k];
}
}
// Search the sub bit stream using KMP
// Caller should ensure the input parameters are legal
int CBitSearch::IndexOfKMP( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start )
{
if( start+tlen > slen ) return -1;
int *next = new int[tlen+1];
if(!next) return -1;
// Get the next value
GetNext(T, tlen, next);
int i = start, j = 0;
while( i < slen && j < tlen )
{
if(j == -1 || GetBit(S,i) == GetBit(T,j)) { ++i; ++j; }
else j = next[j];
}
delete[] next;
if( j == tlen ) return i-j;
else return -1;
}
// Caller should ensure the input parameters are legal
int CBitSearch::IndexOf( const unsigned char* S, const unsigned char* T, int slen, int tlen, int start )
{
if( start+tlen > slen ) return -1;
int i = start, j = 0;
while( i < slen && j < tlen )
{
if(GetBit(S,i) == GetBit(T,j)) { ++i; ++j; }
else { i = i - j + 1; j = 0; }
}
if( j == tlen ) return i-j;
else return -1;
}
// Get the times of sub bit stream appears in source bit stream
int CBitSearch::GetCount( const unsigned char *S, const unsigned char* T, int slen, int tlen, int start )
{
int count = 0;
int pos = 0;
pos = IndexOfKMP( S, T, slen, tlen, start );
while( pos >= 0 )
{
pos = IndexOfKMP( S, T, slen, tlen, pos+tlen );
++count;
}
return count;
}
- KMP算法实现bit搜索
- KMP 字符串搜索算法的c++实现
- C++ 实现搜索子串KMP 算法
- KMP搜索算法
- KMP算法,字符串搜索
- KMP子串搜索算法C语言实现
- 字符串搜索算法kmp与Boyer-Moore,java实现
- bit相关算法实现
- 快速字符串搜索算法KMP
- KMP算法及改进KMP算法实现
- [数据结构]KMP算法实现
- KMP算法的实现
- KMP算法实现
- KMP算法实现
- KMP 算法实现
- kmp算法实现
- KMP算法实现
- KMP算法和实现
- C++ 技巧 (1)
- MSSQL 日志文件总结及日志满的处理
- 读《编程卓越之道:深入理解计算机》
- C++的学习3
- 高手详解木马程序和病毒的原理和清除的通用解法
- KMP算法实现bit搜索
- MS SQL日志文件长度过大的处理方法
- 真实感图形学算法(1)
- “现在程序员多了,XX招四个人不超过1万”
- Web 服务概念性体系结构(WSCA 1.0)WSCA 1.0 第 2 部分
- 电力系统继电保护技术的现状与发展
- 黑盒测试
- 白盒测试
- Java基础 - 接口