hdu2222Keywords 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

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

using namespace std;

struct node
{
    node* next[26];
    int end;
    node* fail;
    node()
    {
        for(int i = 0;i<26;i++)
            next[i] = NULL;
        fail = NULL;
        end = 0;
    }
};
node* root;
char s[51],ss[1100000];
void insert()
{
    int i,l = strlen(s);
    node* k = root;
    for(i  = 0;i<l;i++)
    {
        int id = s[i]-‘a‘;
        if(k->next[id] == NULL)
            k->next[id] = new node();
        k = k->next[id];
    }
    k->end++;
}
void build()
{
    queue<node*> q;
    for(int i = 0;i<26;i++)
    {
        node* k = root;
        if(k->next[i] != NULL)
        {
            k->next[i]->fail = root;
            q.push(k->next[i]);
        }
    }
    while(!q.empty())
    {
        node*k = q.front();
        q.pop();
        for(int i = 0;i<26;i++)
        {
            if(k->next[i]!=NULL)
            {
                node*t = k->fail;
                while(t!=root&&t->next[i] == NULL) t = t->fail;
                if(t->next[i] != NULL) t = t->next[i];
                k->next[i]->fail = t;
                q.push(k->next[i]);
            }
        }
    }
}
int ask()
{
    int i,l = strlen(ss),ans = 0;
    node *k = root;
    for(i = 0;i<l;i++)
    {
        int id = ss[i]-‘a‘;
        while(k!=root&&k->next[id] == NULL) k = k->fail;
        if(k->next[id]!=NULL) k = k->next[id];
        node* t = k;
        while(t!=root)
        {
            ans += t->end;
            t->end = 0;
            t = t->fail;
        }
    }
    return ans;
}

int main()
{
    int z;
    int n,i,j,k;
    cin>>z;
    while(z--)
    {
        root = new node();
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",s);
            insert();
        }
        build();
        scanf("%s",ss);
        printf("%d\n",ask());
    }
    return 0;
}
时间: 2024-08-24 15:59:44

hdu2222Keywords Search的相关文章

hdu2222Keywords 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 t

hdu2222Keywords 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 t

hdu2222-- Keywords Search(AC自动机入门)

Keywords Search Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-05-13) Description In the modern time, Search engine came into the life of everybody like Google, B

hdu2222Keywords Search字典树入门……

#include<iostream> #include<cstring> using namespace std; struct node { int num; node *next[26]; }*root; void join(const char *s) { node *p=root,*t; int i,len=strlen(s); for(i=0;i<len;i++) if(p->next[s[i]-'a']) p=p->next[s[i]-'a']; el

【hdu2222-Keywords Search】AC自动机基础裸题

http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词) 题解: 每个单词末尾节点sum=1:find的时候每个点都顺着fail往上跳,加上该节点的sum,然后将这个sum清了:注意同一个单词出现多次只算一次. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring>

hdu2222--Keywords Search+AC自动机模板

题目链接:点击进入 KMP对模式串进行处理,然后就可以方便的判断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.但是如果模式串有多个,这时如果还用KMP则需要对每个串都进行一次处理,显然不是很合适.其实这时候可以将所有模式串建成一棵trie树,然后采用类似于kmp的方法计算出failed指针,也就可以方便的进行匹配了.其实这就是ac自动机的思想. 代码如下: #include<iostream> #include<cstdio> #include<cstri

每日总结-05-14

为了准备区域赛,决定每天都写一个总结,记录一下今天做了什么事情,如果没做啥有意义的事情,就不总结了. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

是神功盖世

http://ypk.39.net/search/all?k=%A1%E8%D1%CE%CB%E1%C7%FA%C2%ED%B6%E0%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A8x http://ypk.39.net/search/all?k=%A8z%C4%C4%C0%EF%C2%F2%D1%CE%CB%E1%C7%FA%C2%ED%B6%E0Q%A3%BA%A3

如何如何让额呵呵

http://ypk.39.net/search/all?k=%A1%BF%BC%CE%D0%CB%B0%B2%C3%DF%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%F3 http://ypk.39.net/search/all?k=%A1%BF%BA%FE%D6%DD%B0%B2%C3%DF%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4