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

题意:给你n个单词,再给你一个字符串,求单词在 所给字符串出现的个数。

题解:建一颗字典树,从所给字符串的每一个位置当成首字符查找,若找到单词则删掉这个单词。

(注:这题很多用AC自动机写的,字典树可能会超时,用C++交,如果超时,再交几发)

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
#define N 1000010

using namespace std;

int n;
char s[N];

struct Tire {
    int num;
    struct Tire *net[26];
    Tire() {
        num=0;
        for(int i=0; i<26; i++) {
            net[i]=NULL;
        }
    }
};

void CreatTire(Tire *p,char s[]) {
    int i=0;
    Tire *q=p;
    while(s[i]) {
        int nx=s[i]-'a';
        if(q->net[nx]==NULL) {
            q->net[nx]=new Tire;
        }
        q=q->net[nx];
        i++;
    }
    q->num++;
}

int Serch(Tire *p,char s[],int ii) {
    int i=ii;
    Tire *q=p;
    int res=0;
    while(s[i]) {
        int nx=s[i]-'a';
        if(q->net[nx]==NULL)break;
        if(q->net[nx]->num) {
            res+=q->net[nx]->num;
            q->net[nx]->num=0;
        }
        q=q->net[nx];
        i++;
    }
    return res;
}

void Delete(Tire *p) {
    if(p==NULL)return;
    for(int i=0; i<26; i++) {
        Delete(p->net[i]);
    }
    free(p);
}
int main() {
    //freopen("test.in","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        scanf("%d",&n);
        Tire *p=new Tire;
        for(int i=0; i<n; i++) {
            scanf("%s",s);
            CreatTire(p,s);
        }
        scanf("%s",s);
        int ans=0;
        for(int i=0; s[i]; i++) {
            ans+=Serch(p,s,i);
        }
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-09 09:27:05

hdu 2222 Keywords Search(字典树)的相关文章

HDU 2222 - Keywords Search

试个模板- - /* HDU 2222 - Keywords Search [ AC自动机 ] */ #include <bits/stdc++.h> using namespace std; const int N = 500005; const int SIZE = 26; struct Trie { int ch[N][SIZE]; int f[N], last[N], cnt[N], val[N]; int tot, ans; void init() { tot = 0; memset

HDU 2222——Keywords Search(AC自动机)

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

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

HDU 2222 Keywords Search (AC自动机模板题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

HDU 2222 Keywords Search(瞎搞)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 50451    Accepted Submission(s): 16236 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu 2222 Keywords Search(AC自动机入门)

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