1. 程式人生 > >P3181 [HAOI2016]找相同字元

P3181 [HAOI2016]找相同字元

P3181 [HAOI2016]找相同字元

對一個串建SAM,另一個串在這上面跑,到達一點時,假設經過了\(cnt\)個點
計算這個串所有後綴產生的貢獻就好了,直接暴力跑上去可能會超時,topsort預處理一下

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=500000;
LL nod,last,n,T;
LL len[maxn],fail[maxn],son[maxn][26],Ans[maxn],sum[maxn],c[maxn],p[maxn],size[maxn],fval[maxn];
char s[maxn];
inline void Insert(LL c){
    LL np=++nod,p=last;
    len[np]=len[p]+1;
    last=np;
    while(p&&!son[p][c]){
        son[p][c]=np,
        p=fail[p];
    }
    if(!p)
        fail[np]=1;
    else{
        LL q=son[p][c];
        if(len[q]==len[p]+1)
            fail[np]=q;
        else{
            LL nq=++nod;
            len[nq]=len[p]+1;
            fail[nq]=fail[q];
            memcpy(son[nq],son[q],sizeof(son[q]));
            fail[np]=fail[q]=nq;
            while(p&&son[p][c]==q){
                son[p][c]=nq,
                p=fail[p];
            }
        }
    }
    size[last]=1;
}
int main(){ 
    nod=last=1;
    scanf("%s",s);
    LL Len=strlen(s);
    for(LL i=0;i<Len;++i)
        Insert(s[i]-'a');
        
    for(LL i=1;i<=nod;++i)
        c[len[i]]++;
    for(LL i=1;i<=nod;++i)
        c[i]+=c[i-1];
    for(LL i=1;i<=nod;++i)
        p[c[len[i]]--]=i;
    for(LL i=nod;i>=1;--i)
        size[fail[p[i]]]+=size[p[i]];
    for(LL i=1;i<=nod;i++)
        fval[p[i]]=(len[p[i]]-len[fail[p[i]]])*size[p[i]]+fval[fail[p[i]]];
        
    scanf("%s",s);
    LL now=1,cnt=0,ans=0;
    Len=strlen(s);
    for(LL i=0;i<Len;++i){
        LL c=s[i]-'a';
        if(son[now][c])
            ++cnt,
            now=son[now][c];
        else{
            while(now&&!son[now][c])
                now=fail[now];
            if(!now)
                cnt=0,
                now=1;
            else
                cnt=len[now]+1,
                now=son[now][c];
        }
        if(now!=1)
            ans+=fval[fail[now]]+(cnt-len[fail[now]])*size[now];
    }
    printf("%lld",ans);
    return 0;
}/*
fjewiofejhiofjmwopejeugfzjkjnfoakweldnfmoierhguiewkjfkowejrfoiwejsfd
jwierhdwuiek,dedjfkz[pjeowrfhuqigrfwerljfiuekdfkcdfheosf
*/