从Hash Killer I、II、III论字符串哈希

首先,Hash Killer I、II、III是BZOJ上面三道很经典的字符串哈希破解题。当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机水过去,遂恍然大悟。。。

于是,本人今天也做了下实验——假设现在有一个字符串题:输入N,接下来N行输入N个长度一样的由大写字母组成的字符串,求一共有多少种不同的字符串。此题有些类似于Hash Killer上面的原题。首先分析此题本身,两种常规办法:1.建立一棵字典树,然后可以相当方便快捷的判重,对于字符串长度均为M的数据,复杂度O(NM)  2.字符串哈希,选取一对质数pa和pb,哈希值为Sigma((ord(s1[i])-64)*pa^i) mod pb,然后通过哈希值排个序完事

接下来开始——字典树肯定能保证正确这个毫无疑问,但是更加毫无疑问的是哈希是相当容易被卡掉的(HansBug:尤其像Hash Killer II这样素数的神选取我也是醉了),但更加更加毫无疑问的是双取模哈希似乎还比较小强,于是我就此展开实验

1.写出一个数据生成器,负责随机生成N个长度为M的大写字母字符串,然后立刻用Trie树求出答案作为标准输出数据

 type
point=^node;
node=record
ex:longint;
next:array['A'..'Z'] of point;
end;
var
i,j,k,l,m,n,ans:longint;
head:point;
s1,s2:ansistring;
function getpoint:point;inline;
var p:point;c1:char;
begin
new(p);p^.ex:=;
for c1:='A' to 'Z' do p^.next[c1]:=nil;
exit(p);
end;
function check(s1:ansistring):longint;inline;
var i:longint;p:point;
begin
p:=head;
for i:= to length(s1) do
begin
if p^.next[s1[i]]=nil then
p^.next[s1[i]]:=getpoint;
p:=p^.next[s1[i]];
end;
if p^.ex= then
begin
inc(ans);
p^.ex:=ans;
end;
exit(p^.ex);
end;
begin
readln(n,m);
head:=getpoint;ans:=;
RANDOMIZE;
assign(output,'hs.in');
rewrite(output);
writeln(n);
for i:= to n do
begin
s1:='';
for j:= to m do s1:=s1+chr(random()+);
writeln(s1);
check(s1);
end;
close(output);
assign(output,'hss.out');
rewrite(output);
writeln(ans);
close(output);
end.

2.接下来,开始写哈希,也不难,而且代码貌似还略短(这里面两个素数采用互换使用的模式,本程序是双取模的哈希,如果需要改成单值哈希的话直接把第50行去掉即可)

 const pa=;pb=;
var
i,j,k,l,m,n:longint;
ap,bp:array[..] of int64;
a:array[..,..] of int64;
a1,a2,a3,a4:int64;
s1,s2:ansistring;
function fc(a1,a2,a3,a4:int64):longint;inline;
begin
if a1<>a3 then
if a1>a3 then exit() else exit(-)
else
if a2<>a4 then
if a2>a4 then exit() else exit(-)
else exit();
end;
procedure sort(l,r:longint);
var i,j:longint;x,y,z:int64;
begin
i:=l;j:=r;x:=a[(l+r) div ,];y:=a[(l+r) div ,];
repeat
while fc(a[i,],a[i,],x,y)=- do inc(i);
while fc(x,y,a[J,],a[J,])=- do dec(j);
if i<=j then
begin
z:=a[i,];a[i,]:=a[j,];a[j,]:=z;
z:=a[i,];a[i,]:=a[j,];a[j,]:=z;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end; begin
ap[]:=;bp[]:=;
for i:= to do
begin
ap[i]:=(ap[i-]*pa) mod pb;
bp[i]:=(bp[i-]*pb) mod pa;
end;
readln(n);
for i:= to n do
begin
readln(s1);
a[i,]:=;a[i,]:=;
for j:= to length(s1) do
begin
a[i,]:=(a[i,]+ap[j]*(ord(s1[j])-)) mod pb;
a[i,]:=(a[i,]+bp[j]*(ord(s1[j])-)) mod pa; //删除此行即可改为单值哈希
end;
end;
sort(,n);m:=;
a[,]:=-maxlongint;
for i:= to n do if fc(a[i-,],a[i-,],a[i,],a[i,])<> then inc(m);
writeln(m);
readln;
end.

于是开始愉快的用bat来对拍:

1.当N=100000 M=3时,很令人吃惊——单双值的哈希都问题不大(随机跑了403组数据均全部通过)

2.当N=100000 M=100是,果不其然——单值的哈希成功而华丽的实现了0%的命中率,而双值的哈希依然100%(HansBug:实测6001组数据,跑了快两小时有木有啊啊啊啊 wnjxyk:What Ghost? HansBug:我家电脑渣不解释^_^)

(HansBug:呵呵哒BZOJ3098这题我居然上来就WA了,现在看来这究竟是什么样的神人品啊)

结果已经了然,而且从bat上运行的时间来看,当N=100000 M=100时,哈希的速度比trie树看样子明显快——估计是虽然trie树可以达到O(NM),但是假如需要新建大量的点的话,那样势必相当费时,多半慢在这上面了,而哈希就是该怎么玩怎么玩——更重要的是——哈希,绝对不等同于非得开一个巨大的数组瞎搞,比如这个例子中直接排个序就完事啦。更重要的是双值哈希的稳定性还是相当不错滴!!!^_^

后记:以前我曾经一度认为hash算法一定就是必然伴随着一个硕大的数组(HansBug:搞不好还MLE有木有TT  bx2k:那是必然),其实它的灵活性远远超出了我的预想,今天也算是大长了见识;还有祝愿BZOJ3099(Hash Killer III)永远不要有人AC!!!否则那就基本意味着哈希算法的终结了TT

附:对拍用的bat模板,纯手写的哦,如有雷同绝无可能么么哒

 @echo off
set /a s=0
:1
set /a s=s+1
echo Test %s%
rem 此处两个数分别代表N和M,手动修改下即可
echo 10000 100|hs.exe
copy hs.in hash\hs%s%.in >nul
copy hsS.out hash\hs%s%.out >nul
echo.|time
type hash\hs%s%.in|hash.exe >hash\hs%s%.ou
echo.|time
fc hash\hs%s%.ou hash\hs%s%.out >hash\res%s%.txt
fc hash\hs%s%.ou hash\hs%s%.out
goto 1
上一篇:去掉字符串中的空格 JS JQ 正则三种不同写法


下一篇:字符串哈希hash