"NOI2017" earthworms queuing problem-solving report

"NOI2017" earthworms queuing

This question really grass

You consider \ (k \) is so small, each merging two strings to increase the number of useful string is \ (O (k ^ 2) \) , violent join these strings, find what Hash values of these strings, plug Hash table to go inside

Here double hash similar manner, a pull-up table the table, another table stored in the weight value of the string

Then there engage in violence

Sb made a very wrong, I have every earthworm length -1, and it is easy to burst Hash

But I was beginning to open for the length of each string array to engage in a Hash, this big space, big time constant, the card is not in the past, but not because the situation hash burst length of 0

Finally came back, it has been closed since the explosion Hash to ...


Code:

//#pragma GCC optimize("Ofast")
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define ll long long
#define ull unsigned long long
using std::min;
const int SIZE=1<<21;
char ibuf[SIZE],*iS,*iT;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SIZE,stdin),iS==iT?EOF:*iS++):*iS++)
//#define gc() getchar()
template <class T>
void read(T &x)
{
    int f=0;x=0;char c=gc();
    while(!isdigit(c)) f|=c=='-',c=gc();
    while(isdigit(c)) x=x*10+c-'0',c=gc();
    if(f) x=-x;
}
void reads(int *s)
{
    char c=gc();
    while(!isdigit(c)) c=gc();
    while(isdigit(c)) s[++s[0]]=c-'0',c=gc();
}
const int N=300010;
const int mod=19491001;
struct Hash
{
    int head[mod],Next[N*50],siz[N*50],cnt;
    ull idx[N*50];
    void ins(int x,ull id)
    {
        for(int i=head[x];i;i=Next[i])
            if(id==idx[i])
            {
                ++siz[i];
                return;
            }
        Next[++cnt]=head[x],head[x]=cnt;
        siz[cnt]=1;
        idx[cnt]=id;
    }
    void era(int x,ull id)
    {
        for(int i=head[x];i;i=Next[i])
            if(id==idx[i])
            {
                --siz[i];
                return;
            }
        puts("err");
    }
    int qry(int x,ull id)
    {
        for(int i=head[x];i;i=Next[i])
            if(id==idx[i])
                return siz[i];
        return 0;
    }
}Ha;
const int bas=13131;
const int bas2=131;
int n,m,pre[N],suc[N],num[N],s[N];
int saki[233];
int main()
{
    read(n),read(m);
    for(int i=1;i<=n;i++)
    {
        ull idx;
        read(num[i]);
        idx=num[i];
        Ha.ins(num[i],idx);
    }
    for(int op,u,v,k,i=1;i<=m;i++)
    {
        read(op);
        if(op==1)
        {
            read(u),read(v);
            suc[u]=v,pre[v]=u;
            int now=u,l=51,r=50;
            while(now&&l)
            {
                saki[--l]=num[now];
                now=pre[now];
            }
            now=v;
            while(now&&r<=100)
            {
                saki[++r]=num[now];
                now=suc[now];
            }
            for(int i=l;i<=50;i++)
            {
                ull idx=0;
                int x=0;
                for(int j=i;j<=i+49&&j<=r;j++)
                {
                    x=(1ll*x*bas+saki[j])%mod;
                    idx=idx*bas2+saki[j];
                    if(j>50) Ha.ins(x,idx);
                }
            }
        }
        else if(op==2)
        {
            read(u),v=suc[u];
            int now=u,l=51,r=50;
            while(now&&l)
            {
                saki[--l]=num[now];
                now=pre[now];
            }
            now=v;
            while(now&&r<=100)
            {
                saki[++r]=num[now];
                now=suc[now];
            }
            for(int i=l;i<=50;i++)
            {
                ull idx=0;
                int x=0;
                for(int j=i;j<=i+49&&j<=r;j++)
                {
                    x=(1ll*x*bas+saki[j])%mod;
                    idx=idx*bas2+saki[j];
                    if(j>50) Ha.era(x,idx);
                }
            }
            suc[u]=pre[v]=0;
        }
        else
        {
            int ans=1;
            s[0]=0;
            reads(s),read(k);
            int po=1,x=0;ull ba=1,idx=0;
            for(int i=1;i<k;i++) po=1ll*po*bas%mod,ba=ba*bas2;
            for(int i=1;i<k;i++) idx=idx*bas2+s[i],x=(1ll*x*bas+s[i])%mod;
            for(int i=k;i<=s[0];i++)
            {
                idx=idx*bas2+s[i],x=(1ll*x*bas+s[i])%mod;
                ans=1ll*ans*Ha.qry(x,idx)%998244353;
                if(!ans) break;
                idx-=s[i-k+1]*ba;
                x-=1ll*s[i-k+1]*po%mod;
                if(x<0) x+=mod;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

2019.6.1

Guess you like

Origin www.cnblogs.com/butterflydew/p/10959076.html