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

这个题目看到数据规模,第一反应是用字典树来存放。然后每个结点需要一个权值来记录是否是一个关键词的末端,还有一个权值需要判断单词是否被查找过。
但是这个题目需要注意两点(被坑的两点): 1、关键词中出现的重复单词算多个,而且最好在查找的时候,找到一个,其它的都算被找到了。 2、最后查找的时候重复的只算一次。
28M、700MS过的。最好需要释放一下动态内存。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 0x3fffffff

using namespace std;

struct tree
{
    tree *next[26];
    int End;
    bool Endvis;
};

char s[1000005];

void Add(char *str, tree *head)
{
    char *a = str;
    tree *p = head;
    while (a[0] != ‘\0‘)
    {
        int k = a[0]-‘a‘;
        if (p->next[k] == NULL)
        {
            tree *p1;
            p1 = (tree*)malloc(sizeof(tree));
            for (int i = 0; i < 26; ++i)
            {
                p1->next[i] = NULL;
            }
            p1->End = 0;
            p1->Endvis = 0;
            p->next[k] = p1;
        }
        p = p->next[k];
        ++a;
    }
    p->End += 1;
}

int Find (char *str, tree *head)
{
    char *s = str;
    tree *p = head;
    int ans = 0;
    while (s[0] != ‘\0‘)
    {
        int k = s[0]-‘a‘;
        if (p->next[k] == NULL)
        {
            break;
        }
        else
        {
            p = p->next[k];
        }
        if (p->End > 0 && p != head)
        {
            if (p->Endvis == 0)
            {
                ans += p->End;
                p->Endvis = 1;
            }
        }
        ++s;
    }
    if (p == head) return 0;
    return ans;
}

int main()
{
    //freopen ("test.txt", "r", stdin);
    int T;
    scanf ("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        tree *head;
        int n;
        head = (tree*)malloc(sizeof(tree));
        for (int i = 0; i < 26; ++i)
        {
            head->next[i] = NULL;
        }
        scanf ("%d", &n);
        for (int i = 0; i < n; ++i)
        {
            char str[55];
            scanf ("%s", str);
            if (str[0] != ‘\0‘) Add (str, head);
        }
        scanf ("%s", s);
        int len = strlen(s), ans = 0;
        for (int i = 0; i < len; ++i)
        {
            ans += Find(s+i, head);
        }
        printf ("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-09 06:33:23

ACM学习历程—HDU2222 Keywords Search(字典树)的相关文章

hdu----(2222)Keywords Search(trie树)

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

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学习历程—HDU 2795 Billboard(线段树)

Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in th

ACM学习历程—HDU 5289 Assignment(线段树)

Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a gr

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

[ACM] hdu 1251 统计难题 (字典树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

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

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #