求助 SAM
查看原帖
求助 SAM
420129
Nt_Tsumiki楼主2024/4/15 22:09

P5546 是可以过的,本题 #10WA 了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>

using namespace std;
int n;
string s;

struct __SAM {
    #define N 1500001

    int tot=1,np=1,len[N<<3],num[N<<3][6],ch[N<<3][26],fail[N<<3];

    void ins(int c,int op) {
        int p=np;
        len[np=++tot]=len[p]+1,num[np][op]=1;
        while (p and !ch[p][c]) ch[p][c]=np,p=fail[p];
        if (!p) fail[np]=1;
        else {
            int q=ch[p][c];
            if (len[q]==len[p]+1) fail[np]=q;
            else {
                int nq=++tot;
                len[nq]=len[p]+1,fail[nq]=fail[q],fail[q]=fail[np]=nq;
                memcpy(ch[nq],ch[q],sizeof ch[q]);
                while (p and ch[p][c]==q) ch[p][c]=nq,p=fail[p];
            }
        }
    }

    int _cnt,_head[N<<3];
    struct _Node { int to,nxt; }_e[N<<3];

    void add(int x,int y) { _e[++_cnt]=_Node{y,_head[x]},_head[x]=_cnt; }
    void link() { for (int i=1;i<=tot;i++) add(fail[i],i); }
    bool check(int x) { int res=1; for (int i=1;i<=n;i++) res&=num[x][i]; return res; }

    void dfs(int x) {
        for (int i=_head[x];i;i=_e[i].nxt) {
            int y=_e[i].to;
            dfs(y);
            for (int j=1;j<=n;j++) num[x][j]|=num[y][j];
        }
    }
}SAM;

int main() {
    while (cin>>s) {
        int len=s.size(); SAM.np=1; ++n;
        for (int j=0;j<len;j++) SAM.ins(s[j]-'a',n);
    }
    SAM.link(); SAM.dfs(1);
    int ans=0;
    for (int i=2;i<=SAM.tot;i++)
        if (SAM.check(i)) ans=max(ans,SAM.len[i]);
    printf("%d\n",ans);
    return 0;
}
/*
dccccacdcbdbbbadbaa
cdcdbadaaabeddbddae
abdbecbddceaebacaca
debabcaccbbceccdcadd
*/
2024/4/15 22:09
加载中...