HDU-5853 Jong Hyok and String (generalized suffix automata)

HDU-5853 Jong Hyok and String (generalized suffix automata)

Meaning of the questions: Given a number of strings, each time a query string, find all (all string \ (endpos \) are the same with it), (present in a given string) the number of different sub-strings

Generalized suffix automaton?

It is not sound very big on?

Bloggers certainly do not understand the principles, but there are mo relationship

For the substring problem handling multiple strings, each string have joined the same automatic machine, you can get generalized suffix automata (?)

Note that each addition of a string must be \ (lst \) pointers Empty

If you are willing to listen to God to explain the team: References -2015 Team thesis extraction code: 6f4u

\[ \ \]

After the machine is automatically configured generalized suffix

It is actually seeking a given number of sub-string string string corresponding to the original state contained

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

#define reg register
typedef long long ll;
#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=4e5+10;

int n,m;
char s[N];
int trans[N][27],link[N],len[N],stcnt,lst;

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

void Extend(int c) {
    int p=lst,cur=++stcnt;
    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;
            memcpy(trans[clone],trans[q],108);
            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()) {
        n=rd(),m=rd();
        Init();
        rep(i,1,n) {
            lst=0;
            scanf("%s",s+1); int len=strlen(s+1);
            rep(j,1,len) Extend(s[j]-'a');
        } // 构造广义后缀自动机
        printf("Case #%d:\n",kase);
        rep(i,1,m) {
            scanf("%s",s+1);
            int p=0,len=strlen(s+1);
            rep(j,1,len) {
                p=trans[p][s[j]-'a'];
                if(!p) break;
            }
            if(!p) puts("0"); // 不存在该状态
            else printf("%d\n",::len[p]-::len[link[p]]); // 该状态对应的子串数
        }
    }
}






Guess you like

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