zoukankan      html  css  js  c++  java
  • LA4670 Dominating Patterns AC自动机模板

    Dominating Patterns

     

    每次看着别人的代码改成自己的模板都很头大。。。空间少了个0卡了好久

    裸题,用比map + string更高效的vector代替蓝书中的处理方法

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <algorithm>
      6 #include <queue>
      7 #include <vector>
      8 #include <cmath> 
      9 #define min(a, b) ((a) < (b) ? (a) : (b))
     10 #define max(a, b) ((a) > (b) ? (a) : (b))
     11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
     12 inline void swap(int &a, int &b)
     13 {
     14     long long tmp = a;a = b;b = tmp;
     15 }
     16 inline void read(int &x)
     17 {
     18     x = 0;char ch = getchar(), c = ch;
     19     while(ch < '0' || ch > '9') c = ch, ch = getchar();
     20     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
     21     if(c == '-') x = -x;
     22 }
     23 
     24 const int INF = 0x3f3f3f3f;
     25 const int MAXNODE = 150 * 70 * 10;
     26 
     27 int ch[MAXNODE][30], last[MAXNODE], root = 0, tag[MAXNODE], fail[MAXNODE], cnt, tot[MAXNODE];
     28 std::vector<int> node[MAXNODE];
     29 char s[1600][800];
     30 
     31 void insert(int x)
     32 {
     33     int now = root;
     34     for(int i = 1;s[x][i] != '';++ i)
     35     {
     36         int& tmp = ch[now][s[x][i] - 'a' + 1];
     37         if(tmp) now = tmp;
     38         else now = tmp = ++ cnt; 
     39     }
     40     ++ tag[now];
     41     node[now].push_back(x);
     42 }
     43 
     44 int q[MAXNODE], he, ta, ma;
     45 void build()
     46 {
     47     he = 0, ta = 0;
     48     
     49     for(register int i = 1;i <= 26;++ i)
     50     {
     51         int u = ch[root][i];
     52         if(u) q[ta ++] = u, fail[u] = last[u] = 0;
     53     } 
     54     
     55     while(he < ta)
     56     {
     57         int now = q[he ++];
     58         for(register int i = 1;i <= 26;++ i)
     59         {
     60             int u = ch[now][i];
     61             if(!u) 
     62             {
     63                 ch[now][i] = ch[fail[now]][i];
     64                 continue;
     65             }
     66             q[ta ++] = u;
     67             int v = fail[now];
     68             while(v && !ch[v][i]) v = fail[v];
     69             fail[u] = ch[v][i];
     70             last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
     71         }
     72     }
     73 }
     74 
     75 
     76 int n;
     77 char T[1100000];
     78 
     79 void find()
     80 {
     81     int n = strlen(T + 1);
     82     int j = root;
     83     for(register int i = 1;i <= n;++ i)
     84     {
     85         int c = T[i] - 'a' + 1;
     86         j = ch[j][c];
     87         if(tag[j])    for(int k = 0;k < node[j].size();++ k) ++ tot[node[j][k]], ma = max(ma, tot[node[j][k]]);
     88         else if(last[j]) for(int k = 0;k < node[last[j]].size();++ k) ++ tot[node[last[j]][k]], ma = max(ma, tot[node[last[j]][k]]);
     89     }
     90 }
     91 
     92 int main()
     93 {
     94     while(scanf("%d", &n) != EOF && n)
     95     {
     96         ma = 0, memset(tot, 0, sizeof(tot)), memset(ch, 0, sizeof(ch)), memset(tag, 0, sizeof(tag));
     97         for(register int i = 1;i <= cnt;++ i) node[i].clear();
     98         cnt = 1;
     99         for(register int i = 1;i <= n;++ i)
    100         {
    101             scanf("%s", s[i] + 1);
    102             insert(i);
    103         }
    104         build();
    105         scanf("%s", T + 1);
    106         find();
    107         printf("%d
    ", ma);
    108         for(register int i = 1;i <= n;++ i)
    109             if(tot[i] == ma) printf("%s
    ", s[i] + 1);
    110     }
    111     return 0;
    112 }
    LA4670
  • 相关阅读:
    Git 命令 stash cherry-pick reset rebase
    【操作系统】不同语言的线程实现机制对比及数据库锁问题
    【数据结构】搜索二叉树(BST)和普通二叉树的序列化与反序列化
    【自制编译器】读书笔记 -- JavaCC使用的JJ文件格式
    leetcode 874 Robot Simulation
    hihocoder 编程挑战赛75
    浪漫主义的起源--以赛亚柏林
    【美团笔试 2018-4-20 】编程题-1 测例100%通过 欧拉函数求解gcd
    【POJ SOJ HDOJ】HDU 2196 Computer 树中的最长路径
    【Java 核心】多线程笔记
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8321737.html
Copyright © 2011-2022 走看看