HDU-5343 MZL's Circle Zhou (suffix automaton)

HDU-5343 MZL's Circle Zhou (suffix automaton)

The meaning of problems: from \ (A B \,) in each of two strings of a selected sub-string \ (X, Y \) (can be null), can be constructed of different strings required number of

To avoid double counting, we need to provide priority

For any answer in a string, it's legitimate segmentation, we make \ (| x | \) maximum

For \ (A, B \) were constructed suffix automaton

Our statistics \ (y \) at the beginning of the character string is \ (c \) of the number of legal program, it must be such that \ (x \) does not exist \ (+ c \) transfer, otherwise it means \ (| x | \ ) is not the biggest

Of course, \ (B \) To establish an automatic machine backwards to determine the beginning of the substring character

Note that some of the details about the statistical empty string

#include<bits/stdc++.h>
using namespace std;

#define reg register
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)

#define pb push_back
template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); }
template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }

char IO;
template<class T=int> T rd(){
    T s=0;
    int f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar()));
    return f?-s:s;
}

const int N=200000+10;

int n;
char A[N],B[N];
int trans[N][26],link[N],len[N],lst,stcnt,End[N],ma[N];

ull Sum[N],Ans;

void Init(){
    link[0]=-1,len[0]=0;
    rep(i,0,stcnt) ma[i]=0,memset(trans[i],0,104);
    stcnt=lst=0;
}

void Extend(int c) {
    int cur=++stcnt,p=lst;
    End[cur]=len[cur]=len[p]+1;
    while(~p && !trans[p][c]) trans[p][c]=cur,p=link[p];
    if(p==-1) link[cur]=0;
    else {
        int q=trans[p][c];
        if(len[q]==len[p]+1) link[cur]=q;
        else {
            int clone=++stcnt;
            End[clone]=End[q];
            memcpy(trans[clone],trans[q],104);
            len[clone]=len[p]+1,link[clone]=link[q];
            while(~p && trans[p][c]==q) trans[p][c]=clone,p=link[p];
            link[cur]=link[q]=clone;
        }
    }
    lst=cur;
}


int main(){
    rep(kase,1,rd()) {
        scanf("%s%s",A+1,B+1);
        Init();
        rep(i,1,strlen(A+1)) Extend(A[i]-'a');
        memset(Sum,0,sizeof Sum),Ans=0;
        rep(i,1,stcnt) { 
            Ans+=len[i]-len[link[i]];// 考虑y为空串
            rep(j,0,25) if(!trans[i][j]) Sum[j]+=len[i]-len[link[i]]; // 如果不存在转移就累和
        }
        rep(i,0,25) if(!trans[0][i]) Sum[i]++; // 考虑x为空串
        n=strlen(B+1);
        Init();
        drep(i,n,1) Extend(B[i]-'a'); // 要倒着建立自动机才能确定这些子串的开头字符
        rep(i,1,stcnt) if(End[i]) Ans+=(len[i]-len[link[i]])*Sum[B[n-End[i]+1]-'a'];
        Ans++;
        printf("%llu\n",Ans);
    }
}


Guess you like

Origin www.cnblogs.com/chasedeath/p/12216788.html