hdu 2222 Keywords Search(AC自动机模版题)

Keywords Search

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


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代码:
 
AC自动机:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int INF = 2000000000;
const int maxn = 1000005;
const int kind = 26;

typedef struct tire{
    int end;
    tire *next[kind], *fail;
}tire;
tire memory[maxn];
int tot;
char text[maxn], word[55];
tire *create(){
    tire *p;
    p = &memory[tot++];
    for(int i = 0; i < kind; i++) p->next[i] = NULL;
    p->end = 0;
    return p;
}
void insert(tire *p, char *s){
    while(*s)
    {
        int k = *s++ - 'a';
        if(!p->next[k]) p->next[k] = create();
        p = p->next[k];
    }
    p->end++;
}
void build_ac_auto(tire *root){
    queueQ;
    Q.push(root);
    tire *tmp, *p;
    root->fail = NULL;
    while(!Q.empty())
    {
        tmp = Q.front();
        Q.pop();
        for(int i = 0; i < kind; i++)
        {
            if(tmp->next[i])
            {
                if(tmp == root) tmp->next[i]->fail = root;
                else
                {
                    p = tmp->fail;
                    while(p)
                    {
                        if(p->next[i])
                        {
                            tmp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(!p) tmp->next[i]->fail = root;
                }
                Q.push(tmp->next[i]);
            }
        }
    }
}
int query(tire *root, char *s){
    int cnt = 0;
    tire *tmp = root, *t;
    while(*s)
    {
        int k = *s++ - 'a';
        while(!tmp->next[k] && tmp != root) tmp = tmp->fail;
        tmp = tmp->next[k];
        if(!tmp) tmp = root;
        t = tmp;
        while(t != root)
        {
            cnt += t->end;
            t->end = 0;
            t = t->fail;
        }
    }
    return cnt;
}
int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        tot = 0;
        tire *root = create();
        for(int i = 0; i < n; i++)
        {
            scanf("%s", word);
            insert(root, word);
        }
        scanf("%s", text);
        build_ac_auto(root);
        printf("%d\n", query(root, text));
    }
    return 0;
}


 
 
 
tire图:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int INF = 2000000000;
const int maxn = 1000005;
const int kind = 26;

struct node{
    int next[kind], cnt, fail;
    void init(){
        memset(next, 0, sizeof(next));
        fail = -1, cnt = 0;
    }
}T[maxn];
int root, tot, head, tail;
int que[maxn];
int n;
char str[maxn];

void init(){
    root = tot = 0;
    T[root].init();
}
void insert(char *s){
    int p = root, id;
    while(*s)
    {
        id = *s - 'a';
        if(!T[p].next[id])
        {
            T[++tot].init();
            T[p].next[id] = tot;
        }
        p = T[p].next[id];
        s++;
    }
    T[p].cnt++;
}
void build_ac_auto(){
    head = tail = 0;
    que[tail++] = root;
    while(head < tail)
    {
        int u = que[head++];
        for(int i = 0; i < kind; i++)
        {
            if(T[u].next[i])
            {
                int son = T[u].next[i];
                int p = T[u].fail;
                if(u == root) T[son].fail = root;
                else T[son].fail = T[p].next[i];
                que[tail++] = son;
            }
            else //trie图,设定虚拟节点
            {
                int p = T[u].fail;
                if(u == root) T[u].next[i] = root;
                else T[u].next[i] = T[p].next[i];
            }
        }
    }
}
int query(char *s){
    int p = root, cnt = 0;
    while(*s)
    {
        int id = *s - 'a';
        p = T[p].next[id];
        int tmp = p;
        while(tmp != root)
        {
            cnt += T[tmp].cnt;
            T[tmp].cnt = 0;
            tmp = T[tmp].fail;
        }
        s++;
    }
    return cnt;
}
int main()
{
    int t;
    char ss[55];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        init();
        for(int i = 0; i < n; i++)
        {
            scanf("%s", ss);
            insert(ss);
        }
        scanf("%s", str);
        build_ac_auto();
        printf("%d\n", query(str));
    }
    return 0;
}


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