码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2222 Keywords Search

时间:2018-10-14 17:47:28      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:contain   efi   tar   void   bre   user   com   careful   turn   

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\n",Ac_automation(s));
    }
}

 

hdu 2222 Keywords Search

标签:contain   efi   tar   void   bre   user   com   careful   turn   

原文地址:https://www.cnblogs.com/8023spz/p/9786936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!