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个关键字中可以重复。在字符串中,前面出现的关键字后面就不能重加。

#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));
    }
}

hdu2222Keywords Search (字典树),布布扣,bubuko.com

时间: 2024-08-09 18:34:28

hdu2222Keywords Search (字典树)的相关文章

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

hdu 2222 Keywords Search(字典树)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42877    Accepted Submission(s): 13502 Problem Description In the modern time, Search engine came into the life of everybody like

ACM学习历程—HDU2222 Keywords Search(字典树)

Keywords Search 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

LeetCode 211. Add and Search Word - Data structure design(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

ZOJ 3674 Search in the Wiki(字典树 + map + vector)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每个单词都一些tips单词.先输入n个单词和他们的tips.然后m组查询,每次查询一些单词,按字典序输出这些单词的公有tips.(每个单词都都只包含小写大写字母) 思路:对第i个单词,用vector数组g,g[i]来存这个单词的所有tips.对于所有单词建立字典树,在单词的结尾结点存好该单词的tips在g数组中存的一维下标i.最后用map来计数每组询问中

白话算法与数据结构之【字典树】

1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高.      Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子

[算法系列之二十]字典树(Trie)

一 概述 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 二 优点 利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希表高. 三 性质 (1)根节点不包含字符,除根节点外每一个节点都只包含一个字符: (2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串: (3)每个节点的所有子节点包含的字符都不相同. 单词列表为"apps&

字典树

字典树(Trie)是一种很特别的树状信息检索数据结构,如同其名,它的构成就像一本字典,可以让你快速的进行字符插入.字符串搜索等. Trie 一词来自 retrieval,发音为 /tri:/ "tree",也有人读为 /tra?/ "try". 字典树设计的核心思想是空间换时间,所以数据结构本身比较消耗空间.但它利用了字符串的共同前缀(Common Prefix)作为存储依据,以此来节省存储空间,并加速搜索时间.Trie 的字符串搜索时间复杂度为 O(m),m 为最

Trie 字典树

1.UVa 1401 Remember the Word 题意:给出n个字符串集合,问其有多少种组合方式形成目标字符串. 思路:对n个字符串集合建立Trie树,保存每个结点的字符串的顺序编号.然后对这棵树查找目标字符串每一个后缀的前缀字符串,累加. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<vector>