• 【[COCI2011-2012#5] POPLOCAVANJE】


    据说这道题卡空间?

    不存在的,拿(AC)自动机去存(5000 imes5000)的串肯定是要M的

    我们可以考虑对长度为(n)的串建一个(SAM),这样空间就只需要两倍的(3e5)

    之后把每个输入的串放到(SAM)上匹配,匹配到了就打更新一下这个节点的最大匹配长度

    之后在(parent)树上传一下这个最大匹配长度,一遍差分维护就好了

    空间完全不卡,如果硬要继续卡,我们可以直接用(map)来存(SAM)那样空间就是线性的,不需要去乘上那个字符集的常数了

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #define re register
    #define maxn 600006
    #define LL long long
    #define inf 999999999
    inline int max(int a,int b){return a>b?a:b;}
    int tax[maxn],a[maxn];
    int n,m,cnt=1,lst=1,num,ans;
    int fa[maxn],len[maxn],endpos[maxn],son[maxn][26],mx[maxn];
    int pre[maxn>>1];
    char S[5005],T[maxn>>1];
    inline void ins(int c,int o)
    {
        int p=++cnt,f=lst; lst=p;
        len[p]=len[f]+1,endpos[p]=o;
        while(f&&!son[f][c]) son[f][c]=p,f=fa[f];
        if(!f) {fa[p]=1;return;}
        int x=son[f][c];
        if(len[f]+1==len[x]) {fa[p]=x;return;}
        int y=++cnt;
        len[y]=len[f]+1,fa[y]=fa[x],fa[x]=fa[p]=y;
        memcpy(son[y],son[x],sizeof(son[x]));
        while(f&&son[f][c]==x) son[f][c]=y,f=fa[f];
    }
    int main()
    {
        scanf("%d",&n);
        scanf("%s",T+1);
        for(re int i=1;i<=n;i++) ins(T[i]-'a',i);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",S+1);
            int len=strlen(S+1);
            int now=1;
            for(re int i=1;i<=len;i++) {now=son[now][S[i]-'a'];if(!now) break;}
            if(now)mx[now]=max(mx[now],len);
        }
        for(re int i=1;i<=cnt;i++) tax[len[i]]++;
        for(re int i=1;i<=n;i++) tax[i]+=tax[i-1];
        for(re int i=1;i<=cnt;i++) a[tax[len[i]]--]=i;
        for(re int i=1;i<=cnt;i++)
        {
            int k=a[i];
            mx[k]=max(mx[k],mx[fa[k]]);
            if(mx[k]&&endpos[k]) pre[endpos[k]-mx[k]+1]++,pre[endpos[k]+1]--;
        }
        for(re int i=1;i<=n;i++) pre[i]+=pre[i-1];
        for(re int i=1;i<=n;i++) if(!pre[i]) ++ans;
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    IO多路复用
    Elasticsearch之打分机制、集群搭建、脑裂问题
    Elasticsearch之-映射管理,ik分词器
    Elasticsearch之高亮查询,聚合查询
    Elasticsearch之索引、文档、组合查询、排序查询、filter过滤操作
    Go 数组、切片、Maps
    Go if-else语句、for循环、switch语句
    Go 函数、包、mode模式
    毕业设计-1.05
    毕业设计-1.04
  • 原文地址:https://www.cnblogs.com/asuldb/p/10335867.html
Copyright © 2020-2023  润新知