HDU-2222 Keywords Search 【AC自动机】

本文介绍了一种使用AC自动机进行图像检索的方法,通过将关键字插入Trie树并处理失配指针,实现高效的模式匹配。文章详细描述了AC自动机的构建过程,包括关键字的插入、失配指针的获取以及模式串的匹配,旨在提高图像检索系统的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

 
思路:
先把keywords插入Trie,处理出失配指针,再用自动机跑模式串,记录匹配次数时要开一个vis数组,防止多次记录同一结点。
 
 
Code:
  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<string>
  5 #include<vector>
  6 #include<map>
  7 #include<set>
  8 #include<queue>
  9 char str[1000005], s[55];
 10 
 11 using namespace std;
 12 #define M(a, b) memset(a, b, sizeof(a))
 13 const int maxn = 500000 + 5;
 14 
 15 struct AC_mata {
 16     int f[maxn], last[maxn], val[maxn], ch[maxn][26], sz;
 17     int ans;
 18     bool vis[maxn];
 19 
 20     int idx(char c) {return c - 'a';}
 21 
 22     void init() {
 23         ans = 0;
 24         sz = 0;
 25         M(vis, 0);
 26         M(val, 0); M(f, 0); M(last, 0); M(ch, 0);
 27     }
 28 
 29     void insert(char *s) {
 30         int u = 0, len = strlen(s);
 31         for (int i = 0; i < len; ++i) {
 32             int c = idx(s[i]);
 33             if (!ch[u][c]) ch[u][c] = ++sz;
 34             u = ch[u][c];
 35         }
 36         ++val[u];
 37     }
 38 
 39     void getFail() {
 40         queue<int> q;
 41         f[0] = 0;
 42         for (int i = 0; i < 26; ++i) {
 43             int u = ch[0][i];
 44             if (u) {q.push(u); f[u] = 0; last[u] = 0;}
 45         }
 46         while (!q.empty()) {
 47             int r = q.front(); q.pop();
 48             for (int i = 0; i < 26; ++i) {
 49                 int u = ch[r][i];
 50                 if (!u) {ch[r][i] = ch[f[r]][i]; continue;}
 51                 q.push(u);
 52                 int v = f[r];
 53                 while (v && !ch[v][i]) v = f[v];
 54                 f[u] = ch[v][i];
 55                 last[u] = val[f[u]] ? f[u] : last[f[u]];
 56             }
 57         }
 58     }
 59 
 60     void cnt(int j) {
 61         if (j) {
 62             if (!vis[j]) ans += val[j];
 63             vis[j] = 1;
 64             cnt(last[j]);
 65         }
 66     }
 67 
 68     void find(char *T) {
 69         int j = 0, len = strlen(T);
 70         for (int i = 0; i < len; ++i) {
 71             int c = idx(T[i]);
 72             j = ch[j][c];
 73             if (val[j]) cnt(j);
 74             else if (last[j]) cnt(last[j]);
 75         }
 76     }
 77 
 78 };
 79 
 80 AC_mata ac;
 81 
 82 int main() {
 83     ios::sync_with_stdio(false);
 84     int T, n;
 85     cin >> T;
 86     while (T--) {
 87         ac.init();
 88         cin >> n;
 89         for (int i = 0; i < n; ++i) {
 90             cin >> s;
 91             ac.insert(s);
 92         }
 93         ac.getFail();
 94         cin >> str;
 95         ac.find(str);
 96         cout << ac.ans << endl;
 97     }
 98 
 99     return 0;
100 }

 

转载于:https://www.cnblogs.com/robin1998/p/6551613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值