2946: [Poi2000]公共串

一开始理解错了子串的含义,尴尬…
理解了题意.
其实就是个后缀自动机裸题,把第一个串建后缀自动机,然后把所有串放在上面跑即可…
c++代码如下:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i )
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
template<typename T>inline bool chkmax(T&x,T y) { return x < y ? x = y,1: 0; }
template<typename T>inline bool chkmin(T&x,T y) { return x > y ? x = y,1: 0; }
template<typename T>inline void read(T&x)
{
    char c;int sign = 1;x = 0;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c)); 
    x *= sign;
}

const int N = 4e3 + 50;
int p = 1,sz = 1,root = 1;
int f[N],mn[N],ans;
char s[N];
struct SAM { int ch[26],len,pre; }t[N];

inline void insert(int x)
{
    int np = ++ sz;
    mn[np] = t[np].len = t[p].len + 1;
    for(;p && !t[p].ch[x];p = t[p].pre) t[p].ch[x] = np;
    if(!p) t[np].pre = root;
    else
    {
        int q = t[p].ch[x];
        if(t[q].len == t[p].len + 1) t[np].pre = q;
        else
        {
            int nq = ++ sz;
            t[nq] = t[q];
            t[nq].len = t[p].len + 1;
            t[q].pre = t[np].pre = nq;
            for(;p && t[p].ch[x] == q;p = t[p].pre) t[p].ch[x] = nq;
        }
    }
    p = np;
}

int n,l,len,id[N],buc[N];
inline void toposort()
{
    rep(i,1,sz) ++ buc[t[i].len];
    rep(i,1,len) buc[i] += buc[i - 1];
    rep(i,1,sz) id[buc[t[i].len] -- ] = i;
}

int main()
{
    read(n);
    scanf("%s",s + 1);

    len = strlen(s + 1);
    rep(i,1,len) insert(s[i] - 'a');

    toposort();

    rep(ii,2,n)
    {
        memset(f,0,sizeof f);
        scanf("%s",s + 1);
        l = strlen(s + 1);
        int now = 1,le = 0;
        rep(i,1,l)
        {
            int x = s[i] - 'a';
            if(t[now].ch[x]) ++le,now = t[now].ch[x];
            else
            {
                for(;now && !t[now].ch[x];now = t[now].pre) ;
                if(!now) now = root,le = 0;
                else le = t[now].len + 1,now = t[now].ch[x];
            }
            chkmax(f[now],le);
        }

        repd(i,sz,1)
        {
            now = id[i];
            chkmin(mn[now],f[now]);
            chkmax(f[t[now].pre],f[now]);
        }
    }

    rep(i,1,sz) ans = max(ans,mn[i]);

    cout << ans << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值