hdu2222 Keywords Search ac自动机

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222

题目:

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 56558    Accepted Submission(s): 18493

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

思路:ac自动机的模板题啊,我的ac自动机过的第一个题

  不过hdu的测评机好感人,第一交代码500msAC了,,后来由于某些原因再叫同一份代码时居然TLE了!!!再交时间从600-900多的都有。。。真是醉了

  1 #include <queue>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5
  6 struct AC_auto
  7 {
  8     const static int LetterSize = 26;
  9     const static int TrieSize = 26 * ( 1e5 + 50);
 10
 11     int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize];
 12
 13     int newnode(void)
 14     {
 15         memset(next[tot],-1,sizeof(next[tot]));
 16         end[tot] = 0;
 17         return tot++;
 18     }
 19
 20     void init(void)
 21     {
 22         tot = 0;
 23         root = newnode();
 24     }
 25
 26     int getidx(char x)
 27     {
 28         return x - ‘a‘;
 29     }
 30
 31     void insert(char *ss)
 32     {
 33         int len = strlen(ss);
 34         int now = root;
 35         for(int i = 0; i < len; i++)
 36         {
 37             int idx = getidx(ss[i]);
 38             if(next[now][idx] == -1)
 39                 next[now][idx] = newnode();
 40             now = next[now][idx];
 41         }
 42         end[now]++;
 43     }
 44
 45     void build(void)
 46     {
 47         queue<int>Q;
 48         fail[root] = root;
 49         for(int i = 0; i < LetterSize; i++)
 50             if(next[root][i] == -1)
 51                 next[root][i] = root;
 52             else
 53                 fail[next[root][i]] = root,Q.push(next[root][i]);
 54         while(Q.size())
 55         {
 56             int now = Q.front();Q.pop();
 57             for(int i = 0; i < LetterSize; i++)
 58             if(next[now][i] == -1)   next[now][i] = next[fail[now]][i];
 59             else
 60                 fail[next[now][i]] = next[fail[now]][i],Q.push(next[now][i]);
 61         }
 62     }
 63
 64     int match(char *ss)
 65     {
 66         int len,now,res;
 67         len = strlen(ss),now = root,res = 0;
 68         for(int i = 0; i < len; i++)
 69         {
 70             int idx = getidx(ss[i]);
 71             int tmp = now = next[now][idx];
 72             while(tmp)
 73             {
 74                 res += end[tmp];
 75                 end[tmp] = 0;//
 76                 tmp = fail[tmp];
 77             }
 78         }
 79         return res;
 80     }
 81     void debug(void)
 82     {
 83         for(int i = 0;i < tot; i++)
 84         {
 85             printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
 86             for(int j = 0;j < LetterSize;j++)
 87                 printf("%3d",next[i][j]);
 88             printf("]\n");
 89         }
 90     }
 91 };
 92
 93 AC_auto ac;
 94 char sa[1000001];
 95 int main(void)
 96 {
 97     int t,n;
 98     scanf("%d",&t);
 99     while(t--)
100     {
101         ac.init();
102         scanf("%d",&n);
103         while(n--)
104             scanf("%s",sa),ac.insert(sa);
105         ac.build();
106         scanf("%s",sa);
107         printf("%d\n",ac.match(sa));
108     }
109
110     return 0;
111 }
时间: 2024-12-22 05:16:23

hdu2222 Keywords Search ac自动机的相关文章

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

HDU 2222 Keywords Search AC自动机入门题

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

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

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自动机入门题)

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 (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自动机模板题)

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

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

多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <string.h> 5 #define MAX 26 6 7 using namespace std; 8 9 struct node 10 {

hdoj 2222 Keywords Search(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包含的关键字: 假设待查找的字符串为str[0..n],则str[i…j]可能为某一个关键字:假设当前正在匹配字符str[k],则以str[i..k]为关键字的所有可能 可能的关键字的最后一个字符为str[k],使用fail指针进行跳转并判断以str[k]结尾的该结点是否为关键字最后一个结点,重复进行