AC自动机的基本操作

1.询问在文本串中各个询问串各出现了几次
hdu3065

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int T;
int n,sz,ch[500004][34],p[500004],cnt[500004];
char s[1004][54],m[2000004];
int ans[1004];
queue<int>q;
int INS(char *s,int num){
    int l=strlen(s+1),now=1;
    for(int i=1;i<=l;i++){
        int to=s[i]-'A';
        if(ch[now][to])now=ch[now][to];
        else{
            now=ch[now][to]=++sz;
        }
    }
    cnt[now]=num;
    return 0;
}
int build(){
    p[1]=0;
    while(!q.empty())q.pop();
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<26;i++){
            if(!ch[x][i])continue;
            int now=p[x];
            while(!ch[now][i])now=p[now];
            p[ch[x][i]]=ch[now][i];
            q.push(ch[x][i]);
        }
    }
    return 0;
}
int solve(char *pro){
    int now=1,l=strlen(pro+1);
    for(int i=1;i<=l;i++){
        if(pro[i]<'A'||pro[i]>'Z'){
            now=1;
            continue;
        }
        int to=pro[i]-'A';
        while(!ch[now][to])now=p[now];
        now=ch[now][to];
        for(int j=now;j;j=p[j]){
            ans[cnt[j]]++;
        }
    }
    return 0;
}
int w33ha(){
    for(int i=1;i<=sz;i++){
        p[i]=cnt[i]=0;
        for(int j=0;j<26;j++)ch[i][j]=0;
    }
    memset(ans,0,sizeof(ans));
    sz=1;
    for(int i=0;i<26;i++)ch[0][i]=1;
    for(int i=1;i<=n;i++){
        scanf("%s",s[i]+1);INS(s[i],i);
    }
    scanf("%s",m+1);
    build();
    solve(m);
    for(int i=1;i<=n;i++){
        if(ans[i]>0){
            printf("%s: %d\n",s[i]+1,ans[i]);
        }
    }
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d",&n)!=EOF)w33ha();
    return 0;
}

2.询问在文本串中有几个询问串出现过
hdu2222

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int n,sz,ans,ch[1000004][34],p[1000004],cnt[1000004];
char s[54],m[1000004];
bool mark[1000004];
queue<int>q;
int INS(char *s){
    int l=strlen(s+1),now=1;
    for(int i=1;i<=l;i++){
        int to=s[i]-'a';
        if(ch[now][to])now=ch[now][to];
        else{
            now=ch[now][to]=++sz;
        }
    }
    cnt[now]++;
    return 0;
}
int build(){
    p[1]=0;
    while(!q.empty())q.pop();
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<26;i++){
            if(!ch[x][i])continue;
            int now=p[x];
            while(!ch[now][i])now=p[now];
            p[ch[x][i]]=ch[now][i];
            q.push(ch[x][i]);
        }
    }
    return 0;
}
int solve(char *pro){
    int ret=0,now=1,l=strlen(pro+1);
    for(int i=1;i<=l;i++){
        mark[now]=1;
        int to=pro[i]-'a';
        while(!ch[now][to])now=p[now];
        now=ch[now][to];
        if(!mark[now]){
            for(int j=now;j;j=p[j]){
                ret+=cnt[j];
                cnt[j]=0;
            }
        }
    }
    return ret;
}
int w33ha(){
    for(int i=1;i<=sz;i++){
        mark[i]=p[i]=cnt[i]=0;
        for(int j=0;j<26;j++)ch[i][j]=0;
    }
    scanf("%d",&n);
    sz=1;ans=0;
    for(int i=0;i<26;i++)ch[0][i]=1;
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);INS(s);
    }
    scanf("%s",m+1);
    build();
    printf("%d\n",solve(m));
    return 0;
}
int LiangJiaJun(){
    int T;scanf("%d",&T);
    while(T--)w33ha();
    return 0;
}

3.询问在文本串中,各个询问串出现了几次,如果不算公共区间的话,各个询问串出现了几次
zoj3228

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<cstdlib>
#define LiangJiaJun main
using namespace std;
int T;
int n,sz,ch[600004][27],p[600004],cnt[600004];
char m[100004];
vector<int>vec[100004];
struct ASK{
    char s[14];
    int l,type,sit;
}a[100004];
inline bool dex(ASK A,ASK B){
    int l=min(A.l,B.l);
    for(int i=1;i<=l;i++){
        if(i>10)while(1);
        if(A.s[i]!=B.s[i])return A.s[i]<B.s[i];
    }
    return A.l<B.l;
}
bool same(ASK A,ASK B){
     if(A.l!=B.l)return 0;
     for(int i=1;i<=A.l;i++){
         if(A.s[i]!=B.s[i]){
            return 0;
         }
     }
     return 1;
}
int ans[100004][4],ret[100004];
queue<int>q;
int INS(char *s,int num){
    int l=strlen(s+1),now=1;
    for(int i=1;i<=l;i++){
        int to=s[i]-'a';
        if(ch[now][to])now=ch[now][to];
        else{
            now=ch[now][to]=++sz;
        }
    }
    cnt[now]=num;
    return 0;
}
int build(){
    p[1]=0;
    while(!q.empty())q.pop();
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<26;i++){
            if(!ch[x][i])continue;
            int now=p[x];
            while(!ch[now][i])now=p[now];
            p[ch[x][i]]=ch[now][i];
            q.push(ch[x][i]);
        }
    }
    return 0;
}
int solve(char *pro){
    int now=1,l=strlen(pro+1);
    for(int i=1;i<=l;i++){
        int to=pro[i]-'a';
        while(!ch[now][to])now=p[now];
        now=ch[now][to];
        for(int j=now;j;j=p[j]){
            ans[cnt[j]][0]++;
            vec[cnt[j]].push_back(i-a[cnt[j]].l+1);
        }
    }
    for(int i=1;i<=n;i++){
        if(ans[i][0]>0){
            int l=a[i].l,pt=-1;
            for(int j=0;j<vec[i].size();j++){
                if(pt==-1||vec[i][pt]+l-1<vec[i][j]){
                    ans[i][1]++;
                    pt=j;
                }
            }
            vec[i].clear();
        }
    }
    return 0;
}
int w33ha(int CASE){
    for(int i=1;i<=sz;i++){
        p[i]=cnt[i]=0;
        for(int j=0;j<26;j++)ch[i][j]=0;
    }
    memset(ans,0,sizeof(ans));
    memset(ret,0,sizeof(ret));
    sz=1;
    for(int i=0;i<26;i++)ch[0][i]=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].type);
        scanf("%s",a[i].s+1);
        a[i].l=strlen(a[i].s+1);
        a[i].sit=i;
    }
    sort(a+1,a+n+1,dex);
    INS(a[1].s,1);
    for(int i=2;i<=n;i++){
        if(!same(a[i],a[i-1]))INS(a[i].s,i);
    }
    build();
    solve(m);
    for(int i=2;i<=n;i++){
        if(same(a[i],a[i-1])){
            ans[i][0]=ans[i-1][0];
            ans[i][1]=ans[i-1][1];
        }
    }
    for(int i=1;i<=n;i++){
        ret[a[i].sit]=ans[i][a[i].type];
    }
    printf("Case %d\n",CASE);
    for(int i=1;i<=n;i++){
        printf("%d\n",ret[i]);
    }
    puts("");
    return 0;
}
int LiangJiaJun(){
    int T=0;
    while(scanf("%s",m+1)!=EOF)w33ha(++T);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值