HDU2896 - 病毒入侵 - AC自动机
2017-08-18 19:19
197 查看
病毒侵袭
题目链接分类:
data structures
strings
1.题意概述
给你N(1<=N<=500)个模式串,M(1<=M<=1000)个待匹配串,查询这M个串每个串中模式串出现过哪几个,和总的出现次数。2.解题思路
也是裸的AC自动机,因为是有查询几次,为了方便输出,我们插入同时维护记录一下插入的每个模式串的id和次数。3.AC代码
class Trie { public: #define tot_len 100010 #define tot_ch 130 #define CUR s[i] int next[tot_len][tot_ch], fail[tot_len], end[tot_len]; bool used[510]; int root, L; int Newnode() { rep(i, 0, tot_ch) next[L][i] = -1; end[L++] = -1; return L - 1; } void Init() { L = 0; root = Newnode(); } void Insert(char *s, int id) { int len = strlen(s); int now = root; rep(i, 0, len) { if (next[now][CUR] == -1) next[now][CUR] = Newnode(); now = next[now][CUR]; } end[now] = id; } void Build() { queue<int> q; fail[root] = root; rep(i, 0, tot_ch) { if (next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; q.push(next[root][i]); } } while (!q.empty()) { int now = q.front(); q.pop(); rep(i, 0, tot_ch) { if (next[now][i] == -1) next[now][i] = next[fail[now]][i]; else { fail[next[now][i]] = next[fail[now]][i]; q.push(next[now][i]); } } } } bool Query(char *s, int n, int id) { int len = strlen(s); int now = root; memset(used, 0, sizeof used); bool flag = 0; rep(i, 0, len) { now = next[now][CUR]; int tmp = now; while (tmp != root) { if (end[tmp] != -1) { used[end[tmp]] = 1; flag = 1; } tmp = fail[tmp]; } } if (!flag) return 0; printf("web %d:", id); rep(i, 1, n + 1) if (used[i]) printf(" %d", i); puts(""); return 1; } #undef CUR #undef tot_ch #undef tot_len } AC; char ch[maxn], key[291]; inline void solve() { int n, m; while (~scanf("%d", &n)) { AC.Init(); rep(i, 1, n + 1) { scanf("%s", key); AC.Insert(key, i); } AC.Build(); int ans = 0; scanf("%d", &m); rep(i, 1, m + 1) { scanf("%s", ch); if (AC.Query(ch, n, i)) ans++; } printf("total: %d\n", ans); } }
相关文章推荐
- AC自动机(2)--hdu2896(病毒侵袭)(基础题)
- hdu2896(AC自动机模板)
- hdu2896 AC自动机
- hdu2896 AC自动机
- 【AC自动机】hdu2222 hdu2896 hdu3065 zoj3430 poj2778 hdu2243
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
- hdu2896 病毒侵袭 AC自动机入门题
- AC自动机 hdu2896 hdu3065 病毒侵袭
- 【AC自动机】hdu2222 hdu2896 hdu3065 zoj3430 poj2778
- HDU2896[病毒侵袭] AC自动机
- HDU2896 病毒侵袭【AC自动机】
- AC自动机-HDU2896-模板题
- HDU2896-病毒侵袭-ac自动机
- hdu 2896 病毒入侵 AC自动机
- [hdu2896][ac自动机]病毒侵袭
- 【HDU2896】病毒侵袭-AC自动机模板题
- 【AC自动机】hdu2222 hdu2896 hdu3065 zoj3430 poj2778 hdu2243
- 【HDU2896】病毒侵袭——AC自动机基础
- 【HDU2896】病毒侵袭 AC自动机
- hdu2896 AC自动机