hash

一维hash

\[eg: S= "a ,b, c" ; \]

\[hash(S)=1*p^2+2*p+3; \]

unsigned long long p=13331;
unsigned long long hash()
{
    cin>>a;
    int l=strlen(a);
    int res=0;
    for(int i=0;i<l;i++)
    {
        res=(res*p+a[i]);
    }
    return res;
}

二维hash
总共hash两次,一次横向,一次竖向;

unsigned long long hash()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            hsh[i][j]=hsh[i][j-1]*p_1+a[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            hsh[i][j]+=hsh[i-1][j]*p_2;
        }
    }
}

左上角\((x,y)\)右下\((x_1,y_1)\)矩阵\(hash\)值为:

\[hash(x_1,y_1)-hsah(x-1,y1)*p_2 ^{x_1-x+1}-hash(x_1,y-1) \]

\[*p_1^{y_1-y+1}+hash(x-1,y-1)*p_2^{x_1-x+1}*p_1^{y_1-y+1} \]

posted @ 2025-01-22 09:03  流氓兔LMT  阅读(44)  评论(0)    收藏  举报