洛谷P4081 [USACO17DEC]Standing Out from the Herd

链接

点击跳转

题解

把所有的串连在一起,用一些互不相同的奇怪分隔符隔开

统计本质不同的字符串,后缀自动机有着天然的优势,我只需要找出后缀自动机所表示的串当中只出现在一个字符串中的即可

维护 e n d p o s endpos 的最大值和最小值( p a r e n t parent 树上递推即可),如果最大值和最小值所在的字符串相同,那么这个节点所表示的子串就都来自同一个字符串

注意特判边界

水题有若干种做法,这题也可以广义后缀自动机或者后缀数组

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 200010
#define maxe 200010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SAM
{
    int tot, las, fa[maxn<<1], len[maxn<<1], pref[maxn<<1];
    map<int,int> ch[maxn<<1];
    void init()
    {
        int i;
        rep(i,tot)ch[i].clear(),fa[i]=len[i]=pref[i]=0;
        tot=las=1;
    }
    void append(int c)
    {
        int p(las);
        len[las=++tot]=len[p]+1;
        pref[las]=1;
        for(;p and ch[p].find(c)==ch[p].end();p=fa[p])ch[p][c]=las;
        if(!p)fa[las]=1;
        else
        {
            int q=ch[p][c];
            if(len[q]==len[p]+1)fa[las]=q;
            else
            {
                int qq=++tot;
                ch[qq]=ch[q];
                fa[qq]=fa[q];
                len[qq]=len[p]+1;
                fa[q]=fa[las]=qq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
            }
        }
    }
}sam;
struct Graph
{
    int etot, head[maxn<<1], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
ll mx[maxn<<1], mn[maxn<<1], belong[maxn], bg[maxn];
ll N, ans[maxn];
char s[maxn];
void dfs(ll u)
{
    forp(u,G)
    {
        auto v=G.to[p];
        dfs(v);
        mx[u]=max(mx[u],mx[v]);
        mn[u]=min(mn[u],mn[v]);
    }
    if(mx[u]==mn[u])
    {
        ll l=sam.len[sam.fa[u]]+1, r=sam.len[u], t=mn[u]-bg[belong[mn[u]]]+1;
        if(t>=l)ans[belong[mn[u]]] += t-l+1;
    }
    else if(belong[mn[u]]==belong[mx[u]])
    {
        ans[belong[mn[u]]] += sam.len[u] - sam.len[ sam.fa[u] ];
    }
}
int main()
{
    ll i, tot=0, j;
    N = read();
    for(i=1;i<maxn;i++)mx[i]=-linf, mn[i]=linf;
    sam.init();
    rep(i,N)
    {
        scanf("%s",s+1);
        auto len = strlen(s+1);
        bg[i]=tot+1;
        rep(j,len)
        {
            sam.append(s[j]);
            belong[++tot]=i;
            mx[sam.las]=mn[sam.las]=tot;
        }
        sam.append(1000+i);
        belong[++tot]=-1;
        mx[sam.las]=mn[sam.las]=tot;
        rep(j,len)s[j]=0;
    }
    rep(i,sam.tot)if(i>1)G.adde(sam.fa[i],i);
    dfs(1);
    rep(i,N)printf("%lld\n",ans[i]);
    return 0;
}
发布了863 篇原创文章 · 获赞 72 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/103828396