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
Author  lee
Mail Mail 0(0)
Control Panel  Control Panel  
Sign Out  Sign Out
 

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 q;
        fail[0]=0;
        int u=0;
        for(int i=0;i


指针版:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;inext[c])
                p->next[c]=new node();
            p=p->next[c];
        }
        p->val++;
    }
    void getfail()
    {
        queue q;
        node *p=NULL;
        for(int c=0;cnext[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;cnext[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;inext[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;
}


你可能感兴趣的:(AC自动机)