C++之路起航——AC自动机(Keywords Search)

/*
Keywords Search

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

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5
 6 using namespace std;
 7
 8 char s[51],m[1000001];
 9 int T,n,sz,ans;
10 int a[500001][27]/*字典树*/ ,q[500001],point[500001],danger[500001]/*单词结束标志*/;
11 bool mark[500001];
12
13 int ins()
14 {
15 int now=1,l=strlen(s);
16 for (int i=0;i<l;i++)
17 {
18 int t=s[i]-‘a‘+1;
19 if (a[now][t]) now=a[now][t];
20 else now=a[now][t]=++sz;
21 }
22 danger[now]++;
23 }
24
25 int acmach()
26 {
27 q[0]=1;point[1]=0;
28 int now,w=1,t=0;
29 while (t<w)
30 {
31 now=q[t++];
32 for (int i=1;i<=26;i++)
33 {
34 if (!a[now][i]) continue;
35 int k=point[now];
36 while (!a[k][i]) k=point[k];
37 point[a[now][i]]=a[k][i];
38 q[w++]=a[now][i];
39 }
40 }
41 }
42
43 int solve()
44 {
45 int k=1,l=strlen(m);
46 for(int i=0;i<l;i++)
47 {
48 mark[k]=1;
49 int t=m[i]-‘a‘+1;
50 while(!a[k][t]) k=point[k];
51 k=a[k][t];
52 if(!mark[k])
53 for(int j=k;j;j=point[j])
54 {
55 ans+=danger[j];
56 danger[j]=0;
57 }
58 }
59 printf("%d\n",ans);
60 }
61
62 int main()
63 {
64 scanf("%d",&T);
65 while(T--)
66 {
67 ans=0;
68 sz=1;
69 scanf("%d",&n);
70 for(int i=1;i<=26;i++)a[0][i]=1;
71 for (int i=0;i<n;i++)
72 {
73 scanf("%s",s);
74 ins();
75 }
76 scanf("%s",m);
77 acmach();
78 solve();
79 for(int i=1;i<=sz;i++)
80 {
81 point[i]=danger[i]=mark[i]=0;
82 for(int j=1;j<=26;j++)
83 a[i][j]=0;
84 }
85 }
86 }
时间: 2024-12-17 03:46:22

C++之路起航——AC自动机(Keywords Search)的相关文章

C++之路进阶——AC自动机(文本生成器)

F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在Linux平台下,而许多题的数据在Windows下制作,请注意输入.输出语句及数据类型及范围,避免无谓的RE出现. 1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2991  Solved: 1225[Subm

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

Keywords Search (ac 自动机)

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

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 mo

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

HDU2222(Keywords Search,AC自动机)

传送门 Keywords Search Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u 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 imag

hdoj 2222 Keywords Search 【AC自动机 入门题】

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

【HDU2222】Keywords Search(AC自动机)

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