The Preliminary Contest for ICPC Asia Shanghai 2019 G. Substring (sliding window + hash)

 

G. Substring

 

Hey

 

It has been a timeout or super memory

Then change has been changed then the answer is wrong

And then finally concluded that the position does not hash

 

I'm somewhere else to find this:

//https://www.cnblogs.com/napoleon_liu/archive/2010/12/29/1920839.html

uint32_t hash( uint32_t a)
{
   a = (a+0x7ed55d16) + (a<<12);
   a = (a^0xc761c23c) ^ (a>>19);
   a = (a+0x165667b1) + (a<<5);
   a = (a+0xd3a2646c) ^ (a<<9);
   a = (a+0xfd7046c5) + (a<<3); // 
   a = (a^0xb55a4f09) ^ (a>>16); 
   return a;
}

Then I changed to borrow this change

But it ...... I did not go to the modulo cloudy again and then answer with unsigned error

 

Hey learn

Hash pay attention to correct posture, data is power.

 

 

 

Then I did not realize it had previously been

// Ultra Memory: 
IF (MP [the X-]) MP [the X-] ++ ;
 // will not exceed memory 
IF (mp.count (the X-)) MP [the X-] ++;

 

Then ...... hey

 

The idea is the idea of ​​the solution to a problem.

 

Hey

 

Super memory has been timed out

 

The result came out: 3368ms 2132kb

It is entitled to: 10000ms 20480kb

 

 

#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define debug printf("!");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1e5+50;
const int inf=0x3f3f3f3f;

inline ull Gh(ull a)
{
    a+=14271;
    a=(a+0x7ed55d16)+(a<<12);
    a=(a^0xc761c23c)^(a>>19);
    a=(a+0x165667b1)+(a<<5);
    a=(a+0xd3a2646c)^(a<<9);
    a=(a+0xfd7046c5)+(a<<3);
    a=(a^0xb55a4f09)^(a>>16); 
    return a;
}

inline ull Lh(ull a)
{
    a=Gh(a*INT_MAX);
    a=(a+0x7ecc5d16)+(a<<13);
    a=(a^0xc761c89c)^(a>>17);
    a=(a+0x315667b1)+(a<<7);
    a=(a+0xdfa26a6c)^(a<<11);
    a=(a+0xfd7b4aa5)+(a<<5);
    a=(a^0xb59b4e09)^(a>>12); 
    return Gh(a);
}
inline ull Rh(ull a)
{
    a=Gh(a*a+31313);
    a=(a+0x8bd45d31)+(a<<16);
    a=(a^0x24bad623)^(a>>15);
    a=(a+0x971bfa3b)+(a<<7);
    a=(a+0x12abf315)^(a<<4);
    a=(a+0x2e4174bd)+(a<<7);
    a=(a^0xac5baef5)^(a>>14); 
    return Gh(a);
}

char s[maxn],ts[maxn];
tr1::unordered_map<ull,int> mp;
ull anshs[20002];
int veclen[20002];
ull h;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {    
        ll tlen;
        ull L,R,u,u1,u2,unext,chcut,chadd;
        int m,i,j,w,len,nextlen,up,tot=0;
        scanf("%s",s);
        len=strlen(s);
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            scanf("%s",ts);
            tlen=strlen(ts);
            L=ts[0]-'a';R=ts[tlen-1]-'a';
            
            h=Lh(L)+Rh(R);
            for(j=0;j<tlen;j++)
            {
                u=ts[j]-'a';
                h=h+Gh(u);
            }
            mp[h]=1;
            anshs[i]=h;
            veclen[i]=tlen;
        }
        sort(veclen,veclen+m);
        up=unique(veclen,veclen+m)-veclen;
        L=s[0]-'a';R=s[veclen[0]-1]-'a';
        unext=0;
        for(j=0;j<veclen[0];j++)
        {
            u=s[j]-'a';
            unext=unext+Gh(u);
        }
        for(w=0;w<up;w++)
        {
            tlen=veclen[w];
            nextlen=veclen[w+1];
            if(tlen>len)break;
            for(j=0;j+tlen-1<len;j++)//长度滑窗 
            {
                L=s[j]-'a';R=s[j+tlen-1]-'a';
                u1=Lh(L)+Rh(R);
                if(!j)u=unext;
                else
                {
                    chcut=s[j-1]-'a';
                    chadd=R;
                    if(j+tlen-1<nextlen)
                    {
                        unext=unext+Gh(R);
                    }
                    u=u-Gh(chcut)+Gh(chadd);
                }
                if(mp.count(u+u1))mp[u+u1]++;
            }
        }
        for(i=0;i<m;i++)printf("%d\n",mp[anshs[i]]-1);
        mp.clear();
    }
}

 

 

 

Hey...

 

Guess you like

Origin www.cnblogs.com/kkkek/p/11529899.html