字符串HASH模板
程序员文章站
2022-07-15 14:14:41
...
单关键字字符串HASH:
typedef unsigned long long ull;
const ull base = 131;
struct My_Hash
{
ull p[maxn],hs[maxn];
void Insert( char s[] )
{
int len = strlen(s+1);
p[0] = 1,hs[0] = 0;
for ( int i=1 ; i<=len ; i++ )
p[i] = p[i-1]*base,hs[i] = hs[i-1]*base+(ull)s[i];
}
ull GetHash( int l , int r )
{
return (ull)hs[r]-p[r-l+1]*hs[l-1];
}
}S;
双关键字字符串HASH:
typedef unsigned long long ull;
const ull mod1 = 1e9+7;
const ull mod2 = 1e9+9;
const ull base1 = 131;
const ull base2 = 233;
struct My_Hash
{
ull hs1[maxn],p1[maxn];
ull hs2[maxn],p2[maxn];
void Insert( char s[] )
{
int len = strlen(s+1);
hs1[0] = 0,p1[0] = 1;
hs2[0] = 0,p2[0] = 1;
for ( int i=1 ; i<=len ; i++ )
{
p1[i] = p1[i-1]*base1%mod1;
p2[i] = p2[i-1]*base2%mod2;
hs1[i] = ( hs1[i-1]*base1%mod1+(ull)s[i] )%mod1;
hs2[i] = ( hs2[i-1]*base2%mod2+(ull)s[i] )%mod2;
}
}
pair<ull,ull> GetHash( int l , int r )
{
ull tmp1 = hs1[r];
ull tmp2 = hs2[r];
tmp1 = ( tmp1-p1[r-l+1]*hs1[l-1]%mod1+mod1 )%mod1;
tmp2 = ( tmp2-p2[r-l+1]*hs2[l-1]%mod2+mod2 )%mod2;
return make_pair( tmp1 , tmp2 );
}
}S;
上一篇: StarUML2 全平台破解方法
下一篇: 12.17