BZOJ3473&&BZOJ3277串

BZOJ3473&&BZOJ3277串

题面

自己找去

HINT

对于所有串建立一个广义后缀自动机,对于每一个节点开一个set表示这个节点接受的子串在哪些串里出现过,然后在parent tree上做启发式合并,理论复杂度应该是\(nlog_{n}^2\)

广义后缀自动机

这个广义后缀自动机在建立的时候和后缀自动机时是基本差不多的,就是在每加入一个新串的时候,把\(last=root\)就好了

如何找匹配大等于k次

首先因为每个节点的fa表示的子串都是现在这个节点的后缀,所以fa出现的次数一定\(>=\)当前节点出现的次数(这个东西可以感性认知进行证明),另外,如果一个子串出现次数小等于k,在这个子串后面加一个字符后它的出现次数也不可能大于k次

一句话题解

建立一个广义后缀自动机,然后在每个节点上开个set记录在几个串上出现,然后parent tree上启发式合并set,查询答案的时候不断往父亲节点跳,直到满足条件,然后\(\sum{node[u].len}\)

#include<bits/stdc++.h>
#include<set>
using namespace std;
const int maxn=200010;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m;
bool debug;
set<int> s[maxn];
set<int>::iterator it;
struct SUFFIXAUTOMATON{
    struct Node{
        int len,fa;
        map<int,int> ch;
    }node[2000010];
    int lst,root,tot;
    inline void init(){
        lst=root=tot=1;return;
    }
    inline void extend(int now,int id){
        int p=lst;tot++;lst=tot;int np=tot;
        node[np].len=node[p].len+1;s[np].insert(id);
        while(p&&!node[p].ch[now]){
            node[p].ch[now]=np;
            p=node[p].fa;
        }
        if(!p) node[np].fa=1;
        else{
            int q=node[p].ch[now];
            if(node[q].len==node[p].len+1){
                node[np].fa=q;
            }
            else{
                int nq=++tot;node[nq]=node[q];
                node[nq].len=node[p].len+1;
                node[q].fa=nq;node[np].fa=nq;
                while(p&&node[p].ch[now]==q){
                    node[p].ch[now]=nq;
                    p=node[p].fa;
                }
            }
        }
    }
}SAM;
string ch[maxn];
int sum[maxn];
struct Edge{
    int from,to,next;
}edge[maxn*6];
int cnt,head[maxn];
inline void addedge(int u,int v){
    cnt++;
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
inline void dfs(int u){
    for(int i=head[u];i;i=edge[i].next){
        int v=edge[i].to;dfs(v);
        if(s[u].size()<s[v].size()) swap(s[u],s[v]);
        for(it=s[v].begin();it!=s[v].end();it++){
            s[u].insert(*it);
        }
    }
    sum[u]=s[u].size();
    return;
}
int main(){
    n=read();m=read();SAM.init();
    for(int i=1;i<=n;i++){
        cin>>ch[i];int len=ch[i].length();
        for(int j=0;j<len;j++){
            SAM.extend(ch[i][j]-'a'+1,i);
        }
        SAM.lst=1;
    }
    for(int i=1;i<=SAM.tot;i++){
        if(SAM.node[i].fa) addedge(SAM.node[i].fa,i);
    }
    dfs(SAM.root);
    if(m>n){
        for(int i=1;i<=n;i++){
            printf("0 ");
        }
        return 0;
    }
    for(int i=1;i<=n;i++){
        long long ans=0;int u=SAM.root;int len=ch[i].length();
        for(int j=0;j<len;j++){
            u=SAM.node[u].ch[ch[i][j]-'a'+1];
            while(sum[u]<m) u=SAM.node[u].fa;
            ans+=SAM.node[u].len;
        }
        printf("%lld ",ans);
    }
    return 0;
}

你以为这就完了吗,naive

我在写最后那个统计答案的时候,想的做法长这样

for(int i=1;i<=n;i++){
        long long ans=0;int u=SAM.root;int len=ch[i].length();
        for(int j=0;j<len;j++){
            u=SAM.node[u].ch[ch[i][j]-'a'+1];int p=u;
            while(sum[p]<m) p=SAM.node[p].fa;
            ans+=SAM.node[p].len;
        }
        printf("%lld ",ans);
    }

我是想每次匹配完向上跳,但是在bzoj上它TLE了,然后我想了一个妙妙做法,并查集优化

inline int find(int x){
    if(sum[x]>=m) return x;
    else return f[x]=find(f[x]);
}

然后跑得飞快,就过了

猜你喜欢

转载自www.cnblogs.com/wenci/p/10629283.html