• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

AC自动机模板

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define maxn 100005
 7 int last[maxn];
 8 int f[maxn];
 9 struct Trie{
10     int ch[maxn][33];
11     int val[maxn];
12     int sz;
13     Trie(){ sz = 1; memset(ch[0],0,sizeof ch[0]); }
14     void insert(char *p, int v){
15         int u=0,n = strlen(p);
16         for (int i = 0; i < n; i++){
17             int c = p[i] - 'a';
18             if (!ch[u][c]){
19                 memset(ch[sz], 0, sizeof sz);
20                 val[sz] = 0;
21                 ch[u][c] = sz++;
22             }
23             u = ch[u][c];
24         }
25         val[u] = v;
26     }
27     void getfail(){
28         queue<int>q;
29         f[0] = 0;
30         for (int c = 0; c < 33; c++){
31             int u = ch[0][c];
32             if (u){ q.push(u); f[u] = last[u]= 0; }
33         }
34         while (!q.empty()){
35             int r = q.front(); q.pop();
36             for (int c = 0; c < 33; c++){
37                 int u = ch[r][c];
38                 if (!u)continue;
39                 int v = f[r];
40                 while (v&&!ch[v][c])v = f[v];
41                 f[u] = ch[v][c];
42                 last[u] = val[f[u]] ? f[u] : last[f[u]];
43             }
44         }
45     }
46     void print(int i, int j){
47         if (j)printf("%d: %d\n", i, val[j]);
48         print(i, last[j]);
49     }
50     int find(char *s){
51         int n = strlen(s);
52         int j = 0;
53         for (int i = 0; i < n; i++){
54             int c = s[i] - 'a';
55             while (j&&!ch[j][c])j = f[j];
56             j = ch[j][c];
57             if (val[j])print(i, j);
58             else if (last[j])print(i, last[j]);
59         }
60     }
61 };
62 int main(){
63     int t;
64     scanf("%d", &t);
65     return 0;
66 }
View Code 2013-10-18 20:41:20 

 

posted @ 2013-10-18 20:39  HaibaraAi  阅读(74)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3