郁郁青青 长过千寻

boyer-moore

    字符串

  1. BM(X,X.SIZE,Y,Y.SIZE)
  2. GETBC(Y,s,bc)
  3. GETGS(Y,s,gs)
  4. SUFFIXES(Y,s,suf)

BM(X,X.SIZE,Y,Y.SIZE)

1
2
3
4
5
6
7
8
初始化整形数组bc[1057],gs[1057] //bad character,good suffix
初始化整形变量j=0,xl=x.size,yl=y.size
GETBC(y,y.size,bc);
GETGS(y,y.size,gs);
while j<xl-yl
for i=yl-1 downto 0 and y[i]==x[i+j] endfor
if i<0 j=j+gs[0]
else j=j+max(bc[x[i+j]]-yl+1+i,gs[i])

GETBC(Y,s,bc)

1
2
3
4
for i=0 to 255
bc[i]=s
for i=0 to s
bc[y[i]]=s-1-i

GETGS(Y,s,gs)

1
2
3
4
5
6
7
8
9
10
11
12
初始化整形数组suf[1057]
suffixes(y,s,suf)
for i=0 to s-1
gs[i]=s;
j=0
for i=s-1 downto 0
if suf[i]==i+1
for j to s-2-i
if gs[j]=s
gs[j]=s-1-i
for i=0 to m-2
gs[s-1-suf[i]]=s-1-i

SUFFIXES(Y,s,suf)

1
2
3
4
5
6
7
8
9
10
11
12
初始化整形变量f,g=s-1
suf[s-1]=s;
for i=s-2 downto 0
if i>g and suf[i+s-1-f]<i-g
suf[i]=suf[i+s-1-f]
else
if i<g
g=i
f=i
while g>-1 and y[g]==y[g+s-1-f]
g=g-1
suf[i]=f-g
页阅读量:  ・  站访问量:  ・  站访客数: