hdu2896 AC自动机
来源:互联网 发布:淘宝卖家订单管理 编辑:程序博客网 时间:2024/05/16 15:19
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <stack>using namespace std;typedef pair<int, int> pii;typedef long long ll;typedef unsigned long long ull;typedef vector<int> vi;#define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl;#define pri(a) printf("%d\n",(a));#define xx first#define yy second#define sa(n) scanf("%d", &(n))#define sal(n) scanf("%lld", &(n))#define sai(n) scanf("%I64d", &(n))#define rep(i, a, n) for (int i = a; i < n; i++)#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++) const int mod = int(1e9) + 7, INF = 0x3fffffff;const int maxn = 200 * 500 + 12;const int maxcharset = 128;const char charst = 0;char buf[10004];int id;bool used[510];bool flag;struct Trie{ int next[maxn][maxcharset], fail[maxn], ids[maxn]; int root, L; int newnode(void) { for (int i = charst;i < maxcharset;i++) next[L][i] = -1; ids[L++] = -1; return L - 1; } void init(void) { L = 0; root = newnode(); } void insert(char buf[]) { int len = (int)strlen(buf); int now = root; for(int i = 0;i < len;i++) { if(next[now][buf[i] - charst] == -1) next[now][buf[i] - charst] = newnode(); now = next[now][buf[i] - charst]; } ids[now] = id; } void build(void) { queue<int> Q; fail[root] = root; for(int i = 0;i < maxcharset;i++) 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(); for(int i = 0;i < maxcharset;i++) 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]); } } } int query(char buf[]) { memset(used, 0, sizeof(used)); int len = strlen(buf); int now = root; int res = 0; for(int i = 0;i < len;i++) { now = next[now][buf[i] - charst]; int temp = now; while(temp != root) { if (ids[temp] != -1 && !used[ids[temp]]) flag = true, used[ids[temp]] = true; temp = fail[temp]; } } return res; }};Trie ac;int main(void){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif int n, m; while (~sa(n)) { ac.init(); rep (i, 0, n) { id = i + 1; scanf("%s", buf); ac.insert(buf); } sa(m); ac.build(); int total = 0; rep (i, 0, m) { scanf("%s", buf); flag = false; ac.query(buf); if (flag) total++, printf("web %d:", i + 1); rep (j, 1, n + 1) { if (used[j]) flag = true, printf(" %d", j); } if (flag) puts(""); } printf("total: %d\n", total); } return 0;}
0 0
- hdu2896 AC自动机
- hdu2896 AC自动机
- hdu2896之AC自动机
- hdu2896(ac自动机)
- hdu2896 AC自动机
- hdu2896 ac自动机
- hdu2896 & hdu3065 ac自动机
- hdu2896 AC自动机
- hdu2896-ac自动机
- hdu2896(AC自动机)
- hdu2896 AC自动机
- HDU2896【AC自动机-模板】
- HDU2896 AC自动机
- HDU2896-AC自动机
- 【AC自动机】 hdu2896 病毒侵袭
- 【hdu2896】【AC自动机】病毒侵袭
- HDU2896(病毒侵袭--AC自动机)
- hdu2896(AC自动机模板)
- vim升级7.4(启用python支持+遇到的错误)
- EasyUI学习总结(三)——easyloader源码分析
- AndroidStudio gradle使用指南
- JS中关于clientWidth offsetWidth scrollWidth 等的含义
- SSM整合--增删改查
- hdu2896 AC自动机
- BLE-NRF51822教程5-静态密码设置
- 【CSS3】 渐变不可怕
- Linux基础练习_进程
- LeetCode #12 Integer to Roman C# Solution
- 提升gradle对android项目的编译速度
- 综合网管系统监测参数列表
- 最常用的控件ListView
- 给apk重新签名