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

注意:在n个keyword中能够反复。在字符串中,前面出现的keyword后面就不能重加。

#include<stdio.h>
#include<malloc.h>
typedef struct nnn
{
    int flag;
    struct nnn *next[26];
}node;
node *builde()
{
    node *p=(node*)malloc(sizeof(node));
    p->flag=0;
    for(int i=0;i<26; i++)
    p->next[i]=NULL;
    return p;
}
node *root;
void insert(char str[])
{
    node *p=root;
    for(int i=0;str[i]!='\0';i++)
    {
        if(p->next[str[i]-'a']==NULL)
        p->next[str[i]-'a']=builde();
        p=p->next[str[i]-'a'];
    }
    p->flag++;
}
int count(char str[])
{
    int sum=0;
    for(int i=0;str[i]!='\0';i++)
    {
        node *p=root;
        for(int j=i;str[j]!='\0';j++)
        {
            if(p->next[str[j]-'a']==NULL)
                break;
            p=p->next[str[j]-'a'];
            sum+=p->flag; p->flag=0;
        }
    }
    return sum;
}
char str[1000005];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root=builde();
        while(n--)
        {
            scanf("%s",str);
            insert(str);
        }
        scanf("%s",str);
        printf("%d\n",count(str));
    }
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-09-30 18:39:41

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