HDU 2896 病毒侵袭(AC自动机)
来源:互联网 发布:jquery怎么渲染数据 编辑:程序博客网 时间:2024/05/21 08:47
题目链接:点击打开链接
题意:
思路:模板题。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int MAXS = 555 + 10;// & 0x7FFFFFFFconst int seed = 131;const int SIGMA_SIZE = 128;const ll INF64 = ll(1e18);const int maxn = 100;const int MAXNODE = 500*210 + 10;int T,n,m;int ch[MAXNODE][SIGMA_SIZE];int f[MAXNODE]; // fail函数int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的valint last[MAXNODE]; // 输出链表的下一个结点int cnt[MAXS];int sz;map<string, int> ms;void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); ms.clear();}// 字符c的编号// 插入字符串。v必须非0void _insert(char *s, int v) { int u = 0, n = strlen(s); for(int i = 0; i < n; i++) { int c = s[i]; if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = v; // ms[string(s)] = v;} // 递归打印以结点j结尾的所有字符串void print(int j) { if(j) { cnt[val[j]]++; print(last[j]); }}// 在T中找模板bool _find(char* T) { int n = strlen(T); bool ok = false; int j = 0; // 当前结点编号,初始为根结点 for(int i=0;i<n;i++) { // 文本串当前指针 int c = T[i]; while(j && !ch[j][c]) j = f[j]; // 顺着细边走,直到可以匹配 j = ch[j][c]; if(val[j]) print(j), ok = true; else if(last[j]) print(last[j]), ok = true; // 找到了! } return ok;} // 计算fail函数int getFail() { queue<int> q; f[0] = 0; // 初始化队列 for(int c = 0; c < SIGMA_SIZE; c++) { int u = ch[0][c]; if(u) { f[u] = 0; q.push(u); last[u] = 0; } } // 按BFS顺序计算fail while(!q.empty()) { int r = q.front(); q.pop(); for(int c = 0; c < SIGMA_SIZE; c++) { int u = ch[r][c]; if(!u) continue; q.push(u); int v = f[r]; while(v && !ch[v][c]) v = f[v]; f[u] = ch[v][c]; last[u] = val[f[u]] ? f[u] : last[f[u]]; } }}char buf[555][222];char s[11111];int main() {// ios::sync_with_stdio(false); while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) { scanf("%s",buf[i]); _insert(buf[i], i); } getFail(); scanf("%d",&m); int tot = 0; for(int i=1;i<=m;i++) { scanf("%s",s); memset(cnt, 0, sizeof(cnt)); if(_find(s)) { printf("web %d:",i); for(int j=1;j<=n;j++) { if(cnt[j]) printf(" %d",j); } printf("\n"); ++tot; } } printf("total: %d\n",tot); } return 0;}
1 0
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- HDU 2896 - 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(AC自动机)
- HDU 2896 病毒侵袭(ac自动机)
- HDU 2896 病毒侵袭(AC自动机)
- leetcode_069 Sqrt(x)
- HDU 2134 Cuts the cake
- 主题创建者(Custom Theme)
- 代码规范_3:c++ _异常
- 软件工程
- HDU 2896 病毒侵袭(AC自动机)
- 选择排序、插入排序、冒泡排序【O(n^2)】
- [ArrayList]——逻辑梳理
- [leetcode 216] Combination Sum III ------组合数的和(回溯法)
- 第6天-sql简单数据实操
- struts自定义类型转换器,局部和全局
- 且听风吟
- [BZOJ2818] gcd - 欧拉函数+筛法
- Class [org.springframework.transaction.config.TxNamespaceHandler] for namespace