「刷题笔记」AC自动机

1.洛谷 P3808 AC自动机(简单版)

题目来源:https://www.luogu.com.cn/problem/P3808

题意: 给定 n n n 个模式串 s i s_i si 和一个文本串 t t t,求有多少个不同的模式串在文本串里出现过。两个模式串不同当且仅当他们编号不同。

解析: AC自动机入门题,建立 T r i e Trie Trie 树的时候统计下各个模式串的终点经过的次数,然后 b f s bfs bfs 构造 T r i e Trie Trie 树上各个节点的 f a i l fail fail 指针,最后从 T r i e Trie Trie 树的根开始,与文本串一位一位匹配。每走一位,就要找一遍以当前位置为结尾的所有后缀,也就是暴力跳一遍 f a i l fail fail,直到回到根为止,此处也可以剪枝一下,跳过的点标记一下,因为下次不会再走了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define nul string::npos
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
const int maxn=1e6+7;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int t;
int n,m;
char str[maxn];
struct Trie{
    int ch[maxn][26],fail[maxn],vis[maxn],cnt;
    void add(char *s){ //构建 trie 树
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            int v=s[i]-'a';
            if(!ch[now][v])ch[now][v]=++cnt;
            now=ch[now][v];
        }
        vis[now]++; //统计
    }
    void build(){
        queue<int> q;
        for(int i=0;i<26;i++)if(ch[0][i])fail[ch[0][i]]=0,q.push(ch[0][i]);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=0;i<26;i++){
            	if(ch[u][i])fail[ch[u][i]]=ch[fail[u]][i],q.push(ch[u][i]);
            	else ch[u][i]=ch[fail[u]][i];
            }
        }
    }
    int ask(char *s,int ans=0){
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            now=ch[now][s[i]-'a'];
            for(int j=now;j&&~vis[j];j=fail[j])ans+=vis[j],vis[j]=-1;//剪枝一下
        }
        return ans;
    }
}AC;
int main(){
    cin>>n;
    while(n--){
        scanf("%s",str);
        AC.add(str);
    }
    AC.build();
    cin>>str;
    cout<<AC.ask(str)<<'\n';
	return 0;
}

2.洛谷 P3796 AC自动机(加强版)

题目来源: https://www.luogu.com.cn/problem/P3796

题意: n n n 个由小写字母组成的模式串以及一个文本串 t t t。每个模式串可能会在文本串中出现多次。你需要找出哪些模式串在文本串 t t t 中出现的次数最多。

解析: 这道题数据不是很强,建出AC自动机然后每匹配到一个节点就暴力跳 f a i l fail fail,也是可以过的,考虑本题与上一题的区别在于本题的模式串每出现一次就要统计一次,所以每个节点必须跳 f a i l fail fail 一直到根,无法剪枝。考虑一个字符串 s s s 的后缀子串个数显然是 O ( ∣ s ∣ ) O(|s|) O(s) 的,匹配文本串的复杂度是 O ( ∣ t ∣ ) O(|t|) O(t) 的,于是总复杂度 O ( ∣ s ∣ ∣ t ∣ ) O(|s||t|) O(st)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define nul string::npos
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
const int maxn=1e5+7;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,m;
char t[maxn*10];
char str[maxn][75];
int ans[maxn];

struct ACTrie{
    int c[maxn][26],fail[maxn],val[maxn],cnt;
    void init(){
        memset(c,0,sizeof(c));
        memset(val,0,sizeof(val));
        cnt=0;
    }
    void add(char *s,int id){
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            int v=s[i]-'a';
            if(!c[now][v])c[now][v]=++cnt;
            now=c[now][v];
        }
        val[id]=now;
    }
    void build(){
        queue<int> q;
        for(int i=0;i<26;i++)if(c[0][i])fail[c[0][i]]=0,q.push(c[0][i]);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=0;i<26;i++){
                if(c[u][i]){
                    q.push(c[u][i]);
                    fail[c[u][i]]=c[fail[u]][i];
                }
                else c[u][i]=c[fail[u]][i];
            }
        }
    }
    void query(char *s){
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            now=c[now][s[i]-'a'];
            for(int j=now;j;j=fail[j])ans[j]++;
        }
    }
}AC;
int main(){
    while(cin>>n){
        if(!n)break;
        AC.init();
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++){
            scanf("%s",str[i]);
            AC.add(str[i],i);
        }
        AC.build();
        scanf("%s",t);
        AC.query(t);
        int ma=-1;
        for(int i=1;i<=n;i++){
            ma=max(ma,ans[AC.val[i]]);
        }
        printf("%d\n",ma);
        for(int i=1;i<=n;i++){
            if(ans[AC.val[i]]==ma)printf("%s\n",str[i]);
        }
    }
	return 0;
}

3.洛谷 P5357 AC自动机(二次加强版)

题目来源:https://www.luogu.com.cn/problem/P5357

题意: 给你一个文本串 t t t( ∣ t ∣ ≤ 2 × 1 0 6 |t|≤2×10^6 t2×106) 和 n n n 个模式串 s 1.. n s_{1..n} s1..n( ∣ s ∣ ≤ 2 × 1 0 5 |s|≤2×10^5 s2×105),请你分别求出每个模式串 s i s_i si t t t 中出现的次数。

解析: 由于模式串和文本串的长度都较大,还沿用上一题的暴力跳 f a i l fail fail 复杂度为 O ( ∣ s ∣ ∣ t ∣ ) O(|s||t|) O(st) 的做法肯定超时。我们发现,在暴跳的过程中, f a i l fail fail 的形态是一棵树,试想从一个点出发暴跳 f a i l fail fail 的过程,最终肯定会到达 r o o t root root,而在这条路径上某些点会有 1 1 1 的贡献,所以我们不妨建一棵 f a i l fail fail 树,也就是让每个点和它的 f a i l fail fail 指针指向的结点连一条边,把 r o o t root root 作为根,那么每个点暴跳 f a i l fail fail 的路径就在这棵 f a i l fail fail 树上展现的淋漓尽致。又因为我们已经标记了会有贡献的那些点(在建 T r i e Trie Trie 树的时候),所以我们可以在遍历文本串的时候统一把每个点到根的路径贡献都加一,也就是把路径上的点的贡献都统一为 1 1 1,就算有些点本来的贡献不是 1 1 1,但是并不影响我们统计答案。这样的话,我们就可以利用树上差分(单链修改,单点查询)来解决这道题了。差分之后, d f s dfs dfs 一下,子树大小即为该点贡献。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define nul string::npos
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
const int maxn=2e5+7;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,m;
char t[maxn*10];
char str[maxn];
int ans[maxn];

struct Trie{
    int c[maxn][26],fail[maxn],val[maxn],cnt;
    vector<int> g[maxn];
    void add(char *s,int id){
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            int v=s[i]-'a';
            if(!c[now][v])c[now][v]=++cnt;
            now=c[now][v];
        }
        val[id]=now;
    }
    void build(){
        queue<int> q;
        for(int i=0;i<26;i++)if(c[0][i]){
        	fail[c[0][i]]=0,q.push(c[0][i]);
        	g[0].pb(c[0][i]),g[c[0][i]].pb(0);//结点和其fail指向的结点连一条边
        }
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=0;i<26;i++){
                if(c[u][i]){
                    q.push(c[u][i]);
                    fail[c[u][i]]=c[fail[u]][i];
                    g[c[u][i]].pb(fail[c[u][i]]);//结点和其fail指向的结点连一条边
                    g[fail[c[u][i]]].pb(c[u][i]);
                }
                else c[u][i]=c[fail[u]][i];
            }
        }
    }
    void dfs(int u,int fa){ //简单树形dp统计子树大小
        for(int i=0;i<sz(g[u]);i++){
            int v=g[u][i];
            if(v==fa)continue;
            dfs(v,u);
            ans[u]+=ans[v];
        }
    }
    void ask(char *s){
        int len=strlen(s),now=0;
        for(int i=0;i<len;i++){
            now=c[now][s[i]-'a'];
            ans[now]++;//由于根的贡献不用考虑且lca(root,now)=root,所以只让now端贡献加1即可
        }
        dfs(0,-1);
    }
}AC;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%s",str);
        AC.add(str,i);
    }
    AC.build();
    scanf("%s",t);
    AC.ask(t);
    for(int i=1;i<=n;i++){
        printf("%d\n",ans[AC.val[i]]);
    }
	return 0;
}

未完待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值