hdu 2222 Keywords Search

Posted 8023spz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2222 Keywords Search相关的知识,希望对你有一定的参考价值。

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
 

 

Author
Wiskey
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
 
每个样例给n个短字符串,和一个长字符串,问这个长字符串中有几个短字符串出现过,ac自动机模板题。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 1000000
using namespace std;
struct Trie {
    Trie *Next[26],*Fail;
    int sum;
    Trie() {
        for(int i = 0;i < 26;i ++) {
            Next[i] = NULL;
        }
        Fail = NULL;
        sum = 0;
    }
}*root;
void Insert_Str(char *s) {///字符串插入到字典树中
   Trie *r = root;
   int i = -1;
   while(s[++ i]) {
       int d = s[i] - a;
       if(r -> Next[d] == NULL) {
           r -> Next[d] = new Trie();
       }
       r = r -> Next[d];
   }
   r -> sum ++;///结尾加1
}
void Build_Fail() {///通过父结点的Fail更新子结点的Fail
    Trie *node,*temp;
    queue<Trie *> q;
    q.push(root);
    while(!q.empty()) {
        node = q.front();
        q.pop();
        for(int i = 0;i < 26;i ++) {
            if(node -> Next[i]) {///第i个儿子存在
                temp = node -> Fail;///temp赋值当前节点的Fail
                while(temp) {
                    if(temp -> Next[i]) {
                        node -> Next[i] -> Fail = temp -> Next[i];
                        break;
                    }
                    temp = temp -> Fail;
                }
                if(temp == NULL) {///没找到或者本来就是根节点
                    node -> Next[i] -> Fail = root;
                }
                q.push(node -> Next[i]);
            }
        }
    }
}
int Ac_automation(char *s) {
    int i = -1,ans = 0;
    Trie *node = root,*temp;
    while(s[++ i]) {
        int d = s[i] - a;
        while(node != root && node -> Next[d] == NULL) node = node -> Fail;///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点
        if(node -> Next[d]) node = node -> Next[d];
        temp = node;
        while(temp && temp -> sum >= 0) {
            ans += temp -> sum;
            temp -> sum = -1;///出现了  再出现时不再计算
            temp = temp -> Fail;///找最长后缀
        }
    }
    return ans;
}
int main() {
    int t,n;
    char tr[50],s[MAX];
    scanf("%d",&t);
    while(t --) {
        root = new Trie();
        scanf("%d",&n);
        for(int i = 0;i < n;i ++) {
            scanf("%s",tr);
            Insert_Str(tr);
        }
        Build_Fail();
        scanf("%s",s);
        printf("%d
",Ac_automation(s));
    }
}

 

以上是关于hdu 2222 Keywords Search的主要内容,如果未能解决你的问题,请参考以下文章

刷题HDU 2222 Keywords Search

hdu[2222]keywords search

hdu 2222 Keywords Search

HDU - 2222 Keywords Search

hdu 2222 Keywords Search

HDU 2222 - Keywords Search