• HDU 2222 Keywords Search


    没啥可说的,AC自动机模板。模仿lrj和NotOnlySuccess大大写的

    推荐教程o(╯□╰)o http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

      1 #include <cstdio>
      2 #include <sstream>
      3 #include <fstream>
      4 #include <cstring>
      5 #include <iostream>
      6 #include <algorithm>
      7 #include <map>
      8 #include <cctype>
      9 #include <ctime>
     10 #include <set>
     11 #include <climits>
     12 #include <vector>
     13 #include <queue>
     14 #include <stack>
     15 #include <cstdlib>
     16 #include <cmath>
     17 #include <string>
     18 #include <list>
     19 
     20 #define INPUT_FILE "in.txt"
     21 #define OUTPUT_FILE "out.txt"
     22 
     23 using namespace std;
     24 
     25 typedef long long LL;
     26 const int INF = INT_MAX / 2;
     27 
     28 void setfile() {
     29     freopen(INPUT_FILE,"r",stdin);
     30     freopen(OUTPUT_FILE,"w",stdout);
     31 }
     32 
     33 const int maxn = 10000 + 5;
     34 const int maxlen = 51;
     35 const int maxtlen = 1000000 + 5;
     36 const int maxnode = maxn * maxlen;
     37 const int sigma_size = 26;
     38 
     39 struct AC_automation {
     40     int sz;
     41     int ch[maxnode][sigma_size];
     42     int val[maxnode];
     43     int fail[maxnode];
     44 
     45     void init() {
     46         memset(ch[0],0,sizeof(ch[0]));
     47         fail[0] = val[0] = 0;
     48         sz = 1;
     49     }
     50 
     51     inline int idx(char c) {
     52         return c - 'a';
     53     }
     54 
     55     void insert(char *str) {
     56         int len = strlen(str),u = 0;
     57         for(int i = 0;i < len;i++) {
     58             int c = idx(str[i]);
     59             if(ch[u][c] == 0) {
     60                 memset(ch[sz],0,sizeof(ch[sz]));
     61                 fail[sz] = val[sz] = 0;
     62                 ch[u][c] = sz++;
     63             }
     64             u = ch[u][c];
     65         }
     66         val[u]++;
     67     }
     68 
     69     void construct() {
     70         queue<int> q;
     71         int u = 0;
     72         for(int i = 0;i < sigma_size;i++) if(ch[u][i]) {
     73             fail[ ch[u][i] ] = 0;
     74             q.push(ch[u][i]);
     75         }
     76         while(!q.empty()) {
     77             u = q.front(); q.pop();
     78             for(int i = 0;i < sigma_size;i++) {
     79                 int &v = ch[u][i];
     80                 if(v) {
     81                     q.push(v);
     82                     fail[v] = ch[ fail[u] ][i];
     83                 } else {
     84                     v = ch[ fail[u] ][i];
     85                 }
     86             }
     87         }
     88     }
     89 
     90     int query(char *str) {
     91         int len = strlen(str),u = 0;
     92         int ans = 0;
     93         for(int i = 0;i < len;i++) {
     94             int c = idx(str[i]);
     95             u = ch[u][c];
     96             int v = u;
     97             while(v) {
     98                 ans += val[v];
     99                 val[v] = 0;
    100                 v = fail[v];
    101             }
    102         }
    103         return ans;
    104     }
    105 };
    106 
    107 char word[maxlen],T[maxtlen];
    108 AC_automation ac;
    109 
    110 int main() {
    111     int kase; scanf("%d",&kase);
    112     while(kase--) {
    113         ac.init();
    114         int n; scanf("%d",&n);
    115         for(int i = 0;i < n;i++) {
    116             scanf("%s",word);
    117             ac.insert(word);
    118         }
    119         ac.construct();
    120         scanf("%s",T);
    121         printf("%d
    ",ac.query(T));
    122     }
    123     return 0;
    124 }
  • 相关阅读:
    C#密封类和密封方法
    C#类和结构以及堆和栈大烩菜(本来就迷,那就让暴风来的更猛烈吧!)
    C#类、对象、方法、属性详解(摘)感谢博客园,感谢提供者。
    软件工程第十五周总结
    软件工程课堂十一(计算最长英语单词链)
    软件工程第十四周总结
    梦断代码阅读笔记03
    软件工程课堂十(找水王)
    软件工程课堂九(用户体验评价)
    软件工程第十三周总结
  • 原文地址:https://www.cnblogs.com/rolight/p/3656088.html
Copyright © 2020-2023  润新知