[近似匹配] BPR算法

     近似匹配字符串的BPR算法,其实shift-and算法就是BPR算法的一种特例,就是精确匹配版的BPR。

     BPR也是用到位并行技术。

   原理嘛我还是没有很彻底的搞清楚,但是我把代码给写出来了.....

     我的BPR算法C++实现:

 1 int BPR(const char *p, char *t, int k)
 2 {
 3     int Pstr_len,Tstr_len,i,pos;
 4     int R[128],B[256],pR,nR;
 5     Pstr_len = strlen(p);
 6     Tstr_len = strlen(t);
 7     
 8     //Preprocessing
 9     for(i=0;i<256;i++)
10     {
11         B[i]=0;
12     }
13     for(i=0;i<Pstr_len;i++)
14     {
15         B[p[i]] = B[p[i]] | (1<<i);
16     }
17 
18     //Searching
19     for(i=0;i<=k;i++)
20     {
21         R[i] = (int) pow(2.0,i)-1;
22     }
23     for(pos=0;pos<Tstr_len;pos++)
24     {
25         pR = R[0];
26         nR = ((pR<<1)|1)&B[t[pos]];
27         R[0]=nR;
28         for(i=1;i<=k;i++)
29         {
30             nR = ((R[i]<<1)&B[t[pos]])|pR|((pR|nR)<<1);
31             pR = R[i];
32             R[i] = nR;
33         }
34         if((nR&(1<<(Pstr_len-1))))
35         {
36             return 1;
37         }
38     }
39     return 0;
40 }

  当容错率控制在0.1以下且模式串长度在30以内时,用BPR算法配合PEX过滤算法效率更高。

  当容错率在0.1到0.3且模式串长度在0到100之间时,用BPD算法配合PEX过滤算法好。

  当容错率在0.3以上时,就不要用过滤算法了,之间用BPD算法好。

posted on 2012-07-24 11:36  unhealthy  阅读(644)  评论(0编辑  收藏  举报