- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我需要实现一种字符串搜索算法,用于在位文本中查找位模式(匹配项可能不是字节/字对齐的)。对于初学者,我实现了 Boyer-Moore 算法,但比较单个位对于我的目的来说太慢了。因此,我尝试实现一个基于 block 的版本,该版本将比较整个字节/单词,如 this paper 中所述。 ,但它变得复杂且难以管理(部分原因是我不完全理解我在做什么。)
有没有人很好地实现了这样的算法?
我的具体用例是模式长度 N >= 32
,文本窗口 2N
,以及打包到 int
中的位。此外,在这种情况下,N
是字符大小 N % 8 == 0
的倍数。我预处理一次并多次使用更改文本,例如 Boyer-Moore。我只需要第一场比赛。性能是关键。
编辑: 在成功实现 Blocked Boyer-Moore 算法后,我发现没有任何改进(我的一点一点的版本更快!)这可能是我自己的错误,因为我一直在努力我的大脑在上面并将其优化到没有很多评论就没有意义的地步,但它仍然很慢。 Here是的。
最佳答案
我是 SO 社区的新手,但我期待着回馈一些东西。
有趣的问题。我整理了一个实现,它只进行基于字节的比较(借助于预先计算的位模式和位掩码),而不是在比较时执行昂贵的位操作。因此,它应该相当快。它没有实现为 Boyer-Moore algorithm 讨论的任何转换规则(性能优化)从而可以进一步改进。
尽管此实现确实取决于模式位的数量 % CHAR_BIT == 0——在 8 位机器上,满足 N % 8 == 0 的标准,该实现将找到非字节对齐的位模式. (它目前还需要 8 位字符( CHAR_BIT == 8 ),但在极少数情况下您的系统不使用 8 位字符,通过将所有数组/vector 从 uint8_t 更改为 char 并调整它们包含的值以反射(reflect)正确的位数。)
鉴于搜索不进行任何位操作(除了预先计算的字节掩码之外),它的性能应该非常好。
简而言之,指定要搜索的模式,实现将其移位一位并记录移位后的模式。它还计算移位模式的掩码,对于非字节对齐的位模式,比较开始和结束时的一些位将需要被忽略以实现正确的行为。
搜索每个移位位置中的所有模式位,直到找到匹配项或到达数据缓冲区的末尾。
//
// BitStringMatch.cpp
//
#include "stdafx.h"
#include <iostream>
#include <cstdint>
#include <vector>
#include <memory>
#include <cassert>
int _tmain(int argc, _TCHAR* argv[])
{
//Enter text and pattern data as appropriate for your application. This implementation assumes pattern bits % CHAR_BIT == 0
uint8_t text[] = { 0xcc, 0xcc, 0xcc, 0x5f, 0xe0, 0x1f, 0xe0, 0x0c }; //1010 1010, 1010 1010, 1010 1010, 010*1 1111, 1110 0000, 0001 1111, 1110 0000, 000*0 1010
uint8_t pattern[] = { 0xff, 0x00, 0xff, 0x00 }; //Set pattern to 1111 1111, 0000 0000, 1111 1111, 0000 0000
assert( CHAR_BIT == 8 ); //Sanity check
assert ( sizeof( text ) >= sizeof( pattern ) ); //Sanity check
std::vector< std::vector< uint8_t > > shiftedPatterns( CHAR_BIT, std::vector< uint8_t >( sizeof( pattern ) + 1, 0 ) ); //+1 to accomodate bit shifting of CHAR_BIT bits.
std::vector< std::pair< uint8_t, uint8_t > > compareMasks( CHAR_BIT, std::pair< uint8_t, uint8_t >( 0xff, 0x00 ) );
//Initialize pattern shifting through all bit positions
for( size_t i = 0; i < sizeof( pattern ); ++i ) //Start by initializing the unshifted pattern
{
shiftedPatterns[ 0 ][ i ] = pattern[ i ];
}
for( size_t i = 1; i < CHAR_BIT; ++i ) //Initialize the other patterns, shifting the previous vector pattern to the right by 1 bit position
{
compareMasks[ i ].first >>= i; //Set the bits to consider in the first...
compareMasks[ i ].second = 0xff << ( CHAR_BIT - i ); //and last bytes of the pattern
bool underflow = false;
for( size_t j = 0; j < sizeof( pattern ) + 1; ++j )
{
bool thisUnderflow = shiftedPatterns[ i - 1 ][ j ] & 0x01 ? true : false;
shiftedPatterns[ i ][ j ] = shiftedPatterns[ i - 1][ j ] >> 1;
if( underflow ) //Previous byte shifted out a 1; shift in a 1
{
shiftedPatterns[ i ][ j ] |= 0x80; //Set MSb to 1
}
underflow = thisUnderflow;
}
}
//Search text for pattern
size_t maxTextPos = sizeof( text ) - sizeof( pattern );
size_t byte = 0;
bool match = false;
for( size_t byte = 0; byte <= maxTextPos && !match; ++byte )
{
for( size_t bit = 0; bit < CHAR_BIT && ( byte < maxTextPos || ( byte == maxTextPos && bit < 1 ) ); ++bit )
{
//Compare first byte of pattern
if( ( shiftedPatterns[ bit ][ 0 ] & compareMasks[ bit ].first ) != ( text[ byte ] & compareMasks[ bit ].first ) )
{
continue;
}
size_t foo = sizeof( pattern );
//Compare all middle bytes of pattern
bool matchInProgress = true;
for( size_t pos = 1; pos < sizeof( pattern ) && matchInProgress; ++pos )
{
matchInProgress = shiftedPatterns[ bit ][ pos ] == text[ byte + pos ];
}
if( !matchInProgress )
{
continue;
}
if( bit != 0 ) //If compare failed or we're comparing the unshifted pattern, there's no need to compare final pattern buffer byte
{
if( ( shiftedPatterns[ bit ][ sizeof( pattern ) ] & compareMasks[ bit ].second ) != ( text[ byte + sizeof( pattern ) ] & compareMasks[ bit ].second ) )
{
continue;
};
}
//We found a match!
match = true; //Abandon search
std::cout << "Match found! Pattern begins at byte index " << byte << ", bit position " << CHAR_BIT - bit - 1 << ".\n";
break;
}
}
//If no match
if( !match )
{
std::cout << "No match found.\n";
}
std::cout << "\nPress a key to exit...";
std::getchar();
return 0;
}
希望对您有所帮助。
关于c++ - 匹配位串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103438/
使用sed和/或awk,仅在行包含字符串“ foo”并且行之前和之后的行分别包含字符串“ bar”和“ baz”时,我才希望删除行。 因此,对于此输入: blah blah foo blah bar
例如: S1: "some filename contains few words.txt" S2:“一些文件名包含几个单词 - draft.txt” S3:“一些文件名包含几个单词 - 另一个 dr
我正在尝试处理一些非常困惑的数据。我需要通过样本 ID 合并两个包含不同类型数据的大数据框。问题是一张表的样本 ID 有许多不同的格式,但大多数都包含用于匹配其 ID 中某处所需的 ID 字符串,例如
我想在匹配特定屏幕尺寸时显示特定图像。在这种情况下,对于 Bootstrap ,我使用 col-xx-## 作为我的选择。但似乎它并没有真正按照我认为应该的方式工作。 基本思路,我想显示一种全屏图像,
出于某种原因,这条规则 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*
我想做类似的东西(Nemerle 语法) def something = match(STT) | 1 with st= "Summ" | 2 with st= "AVG" =>
假设这是我的代码 var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=123
我怎样才能得到这个字符串的数字:'(31.5393701, -82.46235569999999)' 我已经在尝试了,但这离解决方案还很远:) text.match(/\((\d+),(\d+)\)/
如何去除输出中的逗号 (,)?有没有更好的方法从字符串或句子中搜索 url。 alert(" http://www.cnn.com df".match(/https?:\/\/([-\w\.]+
a = ('one', 'two') b = ('ten', 'ten') z = [('four', 'five', 'six'), ('one', 'two', 'twenty')] 我正在尝试
我已经编写了以下代码,我希望用它来查找从第 21 列到另一张表中最后一行的值,并根据这张表中 A 列和另一张表中 B 列中的值将它们返回到这张表床单。 当我使用下面的代码时,我得到一个工作表错误。你能
我在以下结构中有两列 A B 1 49 4922039670 我已经能够评估 =LEN(A1)如2 , =LEFT(B1,2)如49 , 和 =LEFT(B1,LEN(A1)
我有一个文件,其中一行可以以 + 开头, -或 * .在其中一些行之间可以有以字母或数字(一般文本)开头的行(也包含这些字符,但不在第 1 列中!)。 知道这一点,设置匹配和突出显示机制的最简单方法是
我有一个数据字段文件,其中可能包含注释,如下所示: id, data, data, data 101 a, b, c 102 d, e, f 103 g, h, i // has to do with
我有以下模式:/^\/(?P.+)$/匹配:/url . 我的问题是它也匹配 /url/page ,如何忽略/在这个正则表达式中? 该模式应该: 模式匹配:/url 模式不匹配:/url/page 提
我有一个非常庞大且复杂的数据集,其中包含许多对公司的观察。公司的一些观察是多余的,我需要制作一个键来将多余的观察映射到一个单独的观察。然而,判断他们是否真的代表同一家公司的唯一方法是通过各种变量的相似
我有以下 XML A B C 我想查找 if not(exists(//Record/subRecord
我制作了一个正则表达式来验证潜在的比特币地址,现在当我单击报价按钮时,我希望根据正则表达式检查表单中输入的值,但它不起作用。 https://jsfiddle.net/arkqdc8a/5/ var
我有一些 MS Word 文档,我已将其全部内容转移到 SQL 表中。 内容包含多个方括号和大括号,例如 [{a} as at [b],] {c,} {d,} etc 我需要进行检查以确保括号平衡/匹
我正在使用 Node.js 从 XML 文件读取数据。但是当我尝试将文件中的数据与文字进行比较时,它不匹配,即使它看起来相同: const parser: xml2js.Parser = new
我是一名优秀的程序员,十分优秀!