AC自动机模板(数组+指针)hdu2222

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
 

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34852    Accepted Submission(s): 11219


Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 

Output
Print how many keywords are contained in the description.
 

Sample Input
      
      
1 5 she he say shr her yasherhs
 

Sample Output
      
      
3


今天终于转战AC自动机了

原来看的时候记得有点费劲,都忘得差不多了。今天找了些大神的博客,终于弄懂了

这题基本是模板题,last数组是参考刘汝佳训练指南上的,加上这个比不加快了一倍,他表示沿着失配指针走遇到的第一个单词节点的编号

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000010;
const int maxm=50*10010;
const int SIGMA_SIZE=26;
int n;
char t[60],s[maxn];

struct AC
{
    int ch[maxm][26];
    int val[maxm];
    int fail[maxm],last[maxm];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'a';}
    void insert(char *s)
    {
        int u=0;
        int n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]++;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){q.push(u);fail[u]=0;last[u]=0;}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            for(int i=0;i<SIGMA_SIZE;i++)
            {
                u=ch[r][i];
                if(!u){ch[r][i]=ch[fail[r]][i];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][i])v=fail[v];
                fail[u]=ch[v][i];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    int find(char *s)
    {
        int u=0,cnt=0;
        int n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            u=ch[u][c];
            int temp=0;//必须赋初值为0,表示如果下面两个判断都不成立的时候while可以正常执行
            if(val[u])
                temp=u;
            else if(last[u])
                temp=last[u];
            while(temp)
            {
                cnt+=val[temp];
                val[temp]=0;
                temp=last[temp];
            }
        }
        return cnt;
    }
}tree;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        tree.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",t);
            tree.insert(t);
        }
        tree.getfail();
        scanf("%s",s);
        int ans=tree.find(s);
        printf("%d\n",ans);
    }
    return 0;
}


指针版:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000010;
const int maxm=50*10010;
const int SIGMA_SIZE=26;
int n;
char t[60],s[maxn];
struct node
{
    int val;
    node *next[26];
    node *fail;
    node *last;
    node(){memset(next,NULL,sizeof(next));fail=NULL;last=NULL;val=0;}
};
struct AC
{
    node *root;
    void clear(){root=new node();}
    int idx(char x){return x-'a';}
    void insert(char *s)
    {
        int n=strlen(s);
        node *p=root;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!p->next[c])
                p->next[c]=new node();
            p=p->next[c];
        }
        p->val++;
    }
    void getfail()
    {
        queue<node *> q;
        node *p=NULL;
        for(int c=0;c<SIGMA_SIZE;c++)
        {
            p=root->next[c];
            if(p!=NULL)
            {
                p->fail=p->last=root;
                q.push(p);
            }
        }
        while(!q.empty())
        {
            node *r=q.front();q.pop();
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                p=r->next[c];
                if(p==NULL){r->next[c]=r->fail->next[c];continue;}
                node *v=r->fail;
                while(v&&v->next[c]==NULL){v=v->fail;}
                if(v==NULL)p->fail=root;//加判断
                else p->fail=v->next[c];
                if(p->fail->val)p->last=p->fail;
                else p->last=p->fail->last;
                q.push(p);
            }
        }
    }
    int find(char *s)
    {
        int n=strlen(s);
        node *p=root;
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            p=p->next[c];
            if(!p)p=root;//这里必须加判断,沿失配边走到root,说明不存在这样的匹配,要从root从新开始
            node *temp=NULL;
            if(p->val)temp=p;
            else temp=p->last;
            while(temp!=root&&temp!=NULL)
            {
                cnt+=temp->val;
                temp->val=0;
                temp=temp->last;
            }
        }
        return cnt;
    }
}tree;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        tree.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",t);
            tree.insert(t);
        }
        tree.getfail();
        scanf("%s",s);
        int ans=tree.find(s);
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值