HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.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): 65272 Accepted Submission(s): 21782
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.
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.
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自动机的模板题。模板来自kuangbin, 在此特别鸣谢!
代码如下:
1 #include<bits/stdc++.h> 2 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++) 3 #define ms(a,b) memset((a),(b),sizeof((a))) 4 using namespace std; 5 typedef long long LL; 6 const double EPS = 1e-6; 7 const int INF = 2e9; 8 const LL LNF = 9e18; 9 const int MOD = 1e9+7; 10 const int MAXN = 5e5+10; 11 12 struct Trie //封装在一个结构体中 13 { 14 int next[MAXN][26], fail[MAXN], end[MAXN]; 15 int root, L; 16 17 int newnode() 18 { 19 for(int i = 0; i<26; i++) 20 next[L][i] = -1; 21 end[L++] = 0; 22 return L-1; 23 } 24 25 void init() 26 { 27 L = 0; 28 root = newnode(); 29 } 30 31 void insert(char buf[]) //往Trie树中插入单词 32 { 33 int len = strlen(buf); 34 int now = root; 35 for(int i = 0; i<len; i++) 36 { 37 if(next[now][buf[i]-'a'] == -1) 38 next[now][buf[i]-'a'] = newnode(); 39 now = next[now][buf[i]-'a']; 40 } 41 end[now]++; 42 } 43 44 void build() //构建fail指针 45 { 46 queue<int>Q; 47 fail[root] = root; 48 for(int i = 0; i<26; i++) 49 { 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 } 55 while(!Q.empty()) 56 { 57 int now = Q.front(); 58 Q.pop(); 59 for(int i = 0; i<26; i++) 60 { 61 if(next[now][i] == -1) 62 next[now][i] = next[fail[now]][i]; 63 else 64 fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]); 65 } 66 } 67 } 68 69 int query(char buf[]) //将字符串与Trie树进行匹配 70 { 71 int len = strlen(buf); 72 int now = root; 73 int res = 0; 74 for(int i = 0; i<len; i++) 75 { 76 now = next[now][buf[i]-'a']; 77 int tmp = now; 78 while(tmp != root) 79 { 80 res += end[tmp]; 81 end[tmp] = 0; 82 tmp = fail[tmp]; 83 84 } 85 } 86 return res; 87 } 88 }; 89 90 Trie ac; 91 char buf[MAXN<<1]; 92 int main() 93 { 94 int T, n; 95 scanf("%d",&T); 96 while(T--) 97 { 98 ac.init(); 99 scanf("%d", &n); 100 for(int i = 1; i<=n; i++) 101 { 102 scanf("%s",buf); 103 ac.insert(buf); 104 } 105 ac.build(); 106 scanf("%s",buf); 107 printf("%d\n",ac.query(buf)); 108 } 109 return 0; 110 }