后 LLL 时代哈希
WeLikeStudying · · 个人记录
针对卡哈希 - 洛谷专栏 (luogu.com)所提出的方法设置的哈希方法,试图防止被卡。
咳咳,我也在
已经看到了。LA 群,我要看看你怎么卡的。
Coding and guguguing
Hash Killer-1.0
如果你真的想卡它的话,试试我这个 Hash Killer-1.0 吧,尝试构造两个字符串 You hack it!)。
简单来讲,把字符集
然后你想卡它,但是这既不是 SVP 又不是 CVP,你会怎么卡呢?
#include<bits/stdc++.h>
#define up(a,b,c)for(int a=b;a<=c;++a)
const int p=998244353,q=1e9+7,r=1e8+7,B=114514;
using namespace std;
mt19937 rnd(p);
int g[26];
string s,t;
int H(string s,const int p)
{
int h=0;
for(char c:s)
h=(1ll*h*B+g[c-'a'])%p;
return h;
}
int islow(string s)
{
for(char c:s)
if(!islower(c))return 0;
return 1;
}
int main()
{
up(i,0,25)g[i]=rnd()&2147483647,printf("g[%d]=%d\n",i,g[i]);
cin>>s>>t;
int n=s.size();
if(!islow(s)||!islow(t))puts("you should only contain lowercase letters");
else if(n!=t.size())puts("len not equal");
else
{
printf("mod %d,s:%d t:%d\n",p,H(s,p),H(t,p));
if(H(s,p)!=H(t,p))puts("different");
else
{
printf("mod %d,s:%d t:%d\n",q,H(s,q),H(t,q));
if(H(s,q)!=H(t,q))puts("different");
else
{
printf("mod %d,s:%d t:%d\n",r,H(s,r),H(t,r));
if(H(s,r)!=H(t,r))puts("different");
else
{
if(s==t)puts("s is equal to t");
else puts("You hack it!");
}
}
}
}
}
upd:已经采用 Tree Attack 被 hack!
字符串由链接给出。
方法由博客给出。
Hash Killer-1.1
咳咳,不管怎么说,如果我真的想防止你 hack,我实际上的代码不会这么写,而是会(贴出代码以方便以后打 CF 字符串题的时候 copy):
#include<bits/stdc++.h>
#define up(a,b,c)for(int a=b;a<=c;++a)
const int p=998244353,q=1e9+7,r=1e8+7;
using namespace std;
using namespace chrono;
mt19937 rnd(duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count());
int B;
string s,t;
int H(string s,const int p)
{
int h=0;
for(char c:s)
h=(1ll*h*B+c-'a')%p;
return h;
}
int islow(string s)
{
for(char c:s)
if(!islower(c))return 0;
return 1;
}
int main()
{
B=rnd()&2147483647;
cin>>s>>t;
int n=s.size();
if(!islow(s)||!islow(t))puts("you should only contain lowercase letters");
else if(n!=t.size())puts("len not equal");
else
{
printf("mod %d,s:%d t:%d\n",p,H(s,p),H(t,p));
if(H(s,p)!=H(t,p))puts("different");
else
{
printf("mod %d,s:%d t:%d\n",q,H(s,q),H(t,q));
if(H(s,q)!=H(t,q))puts("different");
else
{
printf("mod %d,s:%d t:%d\n",r,H(s,r),H(t,r));
if(H(s,r)!=H(t,r))puts("different");
else
{
if(s==t)puts("s is equal to t");
else puts("You hack it!");
}
}
}
}
}
基本上你做题的时候用这个就没办法对着卡了,我是预设你没法卡这个的,这只不过是说明字符串哈希没死而已。
咳咳,之前不小心看漏了,让我想想有什么好办法能弄掉那个 Tree Attack,用更正经的方式。