HDU 2222

        一道AC自动机模板题。AC自动机的介绍可以参考AC自动机算法多模字符串匹配算法之AC自动机—原理与实现。如果对第一篇文章中的绿色有向边不太理解的话,可以结合这道题一起理解。
        关于题目本身,需要注意的是,keyword会有重复的字符串,此外,如果一个keyword在description中重复出现的话,只统计一次。此外,还要注意在沿着fail指针回退的时候,也可能遇到keyword。再给出几个测试用例(来自题目讨论区),通过测试用例可以更直观的了解注意事项。

case 1:
3
she
she
she
shesheshe
ans: 3

case 2:
6
a
ba
cba
dcba
baf
f
dcbafd
ans: 6


代码(G++):

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>

#define MAXN 10005
#define MAXLEN 10000005
#define NODE_BUF_SZ 1024
using namespace std;

char keywords[MAXN][51];
char desc_str[MAXLEN];

struct TrieNode{
    char ch;
    TrieNode* childs[26];
    TrieNode* fail;
    //TrieNode* next_match;
    int fin;
    bool vis;

    void set_val(char c)
    {
        ch = c;
        memset(childs, 0, sizeof(childs));
        fail = NULL;
        //next_match = NULL;
        fin = 0;
        vis = false;
    }
};

struct NodeBuffer{
    vector<TrieNode*> p_vec;
    TrieNode* buf;
    int cnt;
    NodeBuffer():buf(NULL), cnt(0){}
    ~NodeBuffer()
    {
        for(size_t i=0; i<p_vec.size(); ++i)
            delete p_vec[i];
        delete buf;
    }
    TrieNode* get_trie_node(char c)
    {
        if(cnt == NODE_BUF_SZ)
        {
            p_vec.push_back(buf);
            buf = NULL;
            cnt = 0;
        }
        if(buf == NULL)
        {
            buf = new TrieNode[NODE_BUF_SZ];
            cnt = 0;
        }

        buf[cnt].set_val(c);
        return &buf[cnt++];
    }
} *node_buf;

TrieNode* build_trie_tree(int n)
{
    TrieNode *root = node_buf->get_trie_node('\0');
    for(int i=0; i<n; ++i)
    {
        TrieNode *node = root;
        int len = strlen(keywords[i]);
        for(int j=0; j<len; ++j)
        {
            int pos = keywords[i][j] - 'a';
            if(node->childs[pos] == NULL)
                node->childs[pos] = node_buf->get_trie_node(keywords[i][j]);
            node = node->childs[pos];
        }
        //printf("%c\n", node->ch);
        ++node->fin;
    }
    return root;
}

TrieNode* get_fail_point(TrieNode* p_fail, TrieNode* child, TrieNode* root)
{
    while(p_fail != NULL)
    {
        int pos = child->ch - 'a';

        if(p_fail->childs[pos] != NULL)
            return p_fail->childs[pos];

        p_fail = p_fail->fail;
    }
    return root;
}

void set_fail_point(TrieNode* root)
{
    queue<TrieNode*> node_q;
    node_q.push(root);
    while(!node_q.empty())
    {
        TrieNode* node = node_q.front();
        node_q.pop();
        TrieNode* p_fail = node->fail;
        for(int i=0; i<26; ++i)
        {
            TrieNode *child = node->childs[i];
            if(child == NULL) continue;
            child->fail = get_fail_point(p_fail, child, root);
            node_q.push(child);
        }
    }
}

TrieNode* build_ac_automation(int n)
{
    TrieNode* root = build_trie_tree(n);
    set_fail_point(root);
    return root;
}

int get_match_count(TrieNode* node)
{
    if(node != NULL && node->fin > 0 && !node->vis)
    {
        node->vis = true;
        return node->fin;
    }
    return 0;
}

int find_by_automation(TrieNode* root, char* content)
{
    int len = strlen(content), cnt = 0;
    TrieNode* node = root;
    for(int i=0; i<len; ++i)
    {
        int pos = content[i] - 'a';
        while(node != NULL)
        {
            if(node->childs[pos] != NULL)
            {
                node = node->childs[pos];
                cnt += get_match_count(node);
                break;
            }else{
                node = node->fail;
                cnt += get_match_count(node);
            }
        }
        if(node == NULL) node = root;
        else{   //为了找到所有被匹配到的关键字,需要主动回退,也可以考虑预处理
            TrieNode* node_bak = node;
            while(node_bak->fail != NULL)
            {
                node_bak = node_bak->fail;
                cnt += get_match_count(node_bak);
            }
        }
    }
    return cnt;
}

int main()
{
    int cases, n;
    scanf("%d", &cases);
    for(int i=0; i<cases; ++i)
    {
        node_buf = new NodeBuffer;
        scanf("%d", &n);
        for(int j=0; j<n; ++j)
            scanf("%s", keywords[j]);
        TrieNode *root = build_ac_automation(n);
        scanf("%s", desc_str);
        int cnt = find_by_automation(root, desc_str);
        printf("%d\n", cnt);
        delete node_buf;
    }
    return 0;
}

题目

Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值