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

 

posted on 2014-11-16 19:20  AndyQsmart  阅读(249)  评论(0编辑  收藏  举报

导航