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 
#include 
#include 
#include 

#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 p_vec;
    TrieNode* buf;
    int cnt;
    NodeBuffer():buf(NULL), cnt(0){}
    ~NodeBuffer()
    {
        for(size_t i=0; iget_trie_node('\0');
    for(int i=0; ichilds[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 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; ichilds[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

题目

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

你可能感兴趣的:(HDUOJ,字符串处理,--------,其他专题,--------)