HUD 2222 AC自动机摸 解法:1.暴力.2.指针型代码.3.数组型代码

http://acm.hdu.edu.cn/showproblem.php?pid=2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 63356    Accepted Submission(s): 21019


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自动机都看了一个星期了。也能自己把模板敲出来了。

分享几篇好的博客http://blog.csdn.net/niushuai666/article/details/7002823(AC自动机)这篇博客访问量60000多,讲的很清楚,刚开始接触看的时候,不追求看懂,主要是他的大概意思,细节问题还是要去推代码才能懂的。

http://blog.csdn.net/v_july_v/article/details/6897097这篇是字典树的博客,只用看字典树的部分。

学AC自动机之前要会KMP(AC自动机需要用的是KMP思想)和字典树。KMP的博客我发的有几篇模板,可以看在这之前的篇。

到现在我就知道AC自动机有两种代码,一种是指针型的,一种是数组型的,前者相对好理解,后者一定要在前者理解后再去理解更好一点(个人认为,我就是这么学的)。

个人感觉这些代码都不是特别难理解,先看博客了解个大概,再一步一步慢慢模拟,用不了多长时间就能懂的。

还有一种暴力过的AC代码:(时间长,但是代码非常短)

StatusAccepted
Time795ms
Memory53496kB
Length1191
LangG++
Submitted

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
struct node
{
    int num;
    node *next[26];
}*root;
void join(const char *s)
{
    node *p=root,*t;
    int i,len=strlen(s);
    for(i=0; i<len; i++)
        if(p->next[s[i]-'a'])
            p=p->next[s[i]-'a'];
        else
        {
            t=new node;
            memset(t,0,sizeof(node));
            p->next[s[i]-'a']=t;
            p=t;
        }
    p->num++;
}
void work(const char *s)
{
    node *p;
    int i,j,len=strlen(s),sum=0;
    for(i=0; i<len; i++)
    {
        p=root;
        for(j=i; j<len; j++)
        {
            if(p->next[s[j]-'a'])
            {
                p=p->next[s[j]-'a'];
                sum+=p->num;
                p->num=0;
            }
            else
                break;
        }
    }
    printf("%d\n",sum);
}
void init()
{
    int num;
    char t[60];
    root=new node;
    memset(root,0,sizeof(node));
    scanf("%d",&num);
    while(num--)
    {
        scanf("%s",t);
        join(t);
    }
}
int main()
{
    int exp;
    char t[1000010];
    scanf("%d",&exp);
    while(exp--)
    {
        init();
        scanf("%s",t);
        work(t);
    }
}


指针型的AC代码:(找了很多代码,就这个代码运行时间最短,代码不算长)

StatusAccepted
Time234ms
Memory53428kB
Length2114
LangG++
Submitted

#include <iostream>
#include <queue>
#include<cstring>
#include<cstdio>
using namespace std;

struct Trie
{
    int    cnt;
    Trie*  next[26], *prefix;
} Table[500000];

int   index= 0;
Trie* root;
char  str[1000001];

void init()
{
    index= 0;
    root= &Table[index];
    memset( Table[0].next, 0, sizeof(Table[0].next) );
    Table[0].cnt= 0;
    Table[0].prefix= NULL;
}

void insert( char* s )
{
    Trie* r= root;
    while( *s )
    {
        int t= *s- 'a';
        if( !r->next[t] )
        {
            ++index;
            memset( Table[index].next, 0, sizeof(Table[index].next) );
            Table[index].cnt= 0;
            Table[index].prefix= NULL;
            r->next[t]= &Table[index];
        }
        r= r->next[t];
        s++;
    }
    r->cnt++;
}

void get_prefix()
{
    queue<Trie*> q;
    q.push( root );
    root->prefix= NULL;

    while( !q.empty() )
    {
        Trie* father= q.front();
        q.pop();
        for( int i= 0; i< 26; ++i )
            if( father->next[i] )
            {
                Trie* tmp= father->prefix;
                while( tmp && !tmp->next[i] ) tmp= tmp->prefix;
                if( !tmp ) father->next[i]->prefix= root;
                else       father->next[i]->prefix= tmp->next[i];

                q.push( father->next[i] );
            }
    }
}

int ac_run()
{
    Trie* p= root;
    int ans= 0;
    char* s= str;
    while( *s )
    {
        int t= *s- 'a';
        while( !p->next[t] && p!= root ) p= p->prefix;
        p= p->next[t];
        if( !p ) p= root;
        Trie* tp= p;
        while( tp!= root && tp->cnt!= -1 )
        {
            ans+= tp->cnt;
            tp->cnt= -1;
            tp= tp->prefix;
        }
        s++;
    }
    return ans;
}

int main()
{
    int test;
    scanf("%d",&test );
    while( test-- )
    {
        int n;
        char s[55];
        scanf("%d",&n );
        init();
        getchar();
        for( int i= 0; i< n; ++i )
        {
            gets(s);
            insert(s);
        }
        gets(str);
        get_prefix();
        printf("%d\n", ac_run() );
    }

    return 0;
}



数组型的AC代码:(个人认为数组型的代码跟简单一些,能A掉卡内存的题(指针型的一直是超内存,所以我才学会了数组型的代码0.0))

StatusAccepted
Time327ms
Memory65856kB
Length1667
LangG++
Submitted

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define N 500005
char s[N*2];
int n,ans,T,sz;
int fail[N],ch[N][30],value[N];
bool vis[N];
queue<int> q;
void init()
{
    ans = sz = 0;
    memset(fail,0,sizeof(fail));
    memset(value,0,sizeof(value));
    memset(ch,0,sizeof(ch));
    memset(vis,0,sizeof(vis));
}
void insert()
{
    int len = strlen(s);
    int now = 0;
    for( int i = 0; i < len; i++ )
    {
        int x = s[i] - 'a';
        if( !ch[now][x] ) ch[now][x] = ++sz;
        now = ch[now][x];
    }
    ++value[now];
}
void fal()
{
    while( !q.empty() ) q.pop();
    for( int i = 0; i < 26; i++ )
        if( ch[0][i] ) q.push(ch[0][i]);
    while( !q.empty() )
    {
        int now = q.front();
        q.pop();
        for( int i = 0; i < 26; i++ )
        {
            if( !ch[now][i] )
            {
                ch[now][i] = ch[fail[now]][i];
                continue;
            }
            fail[ch[now][i]] = ch[fail[now]][i];
            q.push(ch[now][i]);
        }
    }
}
void ac()
{
    int len = strlen(s);
    int now = 0;
    for( int i = 0; i < len; i++ )
    {
        int x = s[i] - 'a';
        int y = ch[now][x];
        while( y && !vis[y] )
        {
            vis[y] = 1;
            ans += value[y];
            y = fail[y];
        }
        now = ch[now][x];
    }
}
int main()
{
    scanf("%d", &T);
    while( T-- )
    {
        init();
        scanf("%d", &n);
        getchar();
        while( n-- )
        {
            gets(s);
            insert();
        }
        gets(s);
        fal();
        ac();
        printf("%d\n", ans);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值