zoukankan      html  css  js  c++  java
  • [模板]洛谷T3808 AC自动机(简单版)

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<ctime>
      6 #include<cstdlib>
      7 
      8 #include<string>
      9 #include<stack>
     10 #include<queue>
     11 #include<vector>
     12 #include<algorithm>
     13 #include<map>
     14 #include<set>
     15 
     16 #define inf 2147483647
     17 #define ri register int
     18 #define ll long long
     19 
     20 using namespace std;
     21 
     22 inline void read(int &x){
     23     x=0;
     24     char t=getchar();
     25     bool f=0;
     26     
     27     while(t<'0' || t>'9'){
     28         if(t=='-')f=1;
     29         t=getchar();
     30     }
     31     
     32     while(t>='0' && t<='9'){
     33         x=(x<<3)+(x<<1)+t-'0';
     34         t=getchar();
     35     }
     36     
     37     if(f)x=-x;
     38 }
     39 
     40 inline int idx(char t){
     41     return t-'a';
     42 }
     43 
     44 struct que{
     45     int q[1000005];
     46     int head,tail;
     47     
     48     inline void clear(){
     49         head=tail=0;
     50     }
     51     
     52     inline bool emp(){
     53         return head==tail;
     54     }
     55     
     56     inline void push(int x){
     57         q[tail]=x;
     58         tail++;
     59         if(tail==1000005)tail=0;
     60     }
     61     
     62     inline void pop(){
     63         head++;
     64         if(head==1000005)head=0;
     65     }
     66     
     67     inline int top(){
     68         return q[head];
     69     }
     70 };
     71 
     72 struct node{
     73     int ch[26];
     74     int val;
     75     int next;
     76     int last;
     77     
     78     inline void make(){
     79         memset(ch,0,sizeof(ch));
     80         val=next=last=0;
     81     }
     82 };
     83 
     84 inline void insert();
     85 inline void bfs();
     86 void work(int);
     87 
     88 node trie[1000005];
     89 int pn=1;
     90 
     91 que q;
     92 
     93 char S[1000005];
     94 int lens;
     95 char T[1000005];
     96 int lent;
     97 
     98 int n,poi=0,c,ans=0;
     99 
    100 int main(){
    101     trie[0].make();
    102     
    103     read(n);
    104     
    105     for(ri i=1;i<=n;i++){
    106         scanf("%s",S+1);
    107         lens=strlen(S+1);
    108         insert();
    109     }
    110     
    111     bfs();
    112     
    113     scanf("%s",T+1);
    114     lent=strlen(T+1);
    115     
    116     for(ri i=1;i<=lent;i++){
    117         c=idx(T[i]);
    118         
    119         while(poi && !trie[poi].ch[c])poi=trie[poi].next;
    120         poi=trie[poi].ch[c];
    121         
    122         work(poi);
    123     }
    124     
    125     printf("%d
    ",ans);
    126     
    127     return 0;
    128 }
    129 
    130 inline void insert(){
    131     int u=0,c;
    132     
    133     for(ri i=1;i<=lens;i++){
    134         c=idx(S[i]);
    135         
    136         if(!trie[u].ch[c]){
    137             trie[pn].make();
    138             trie[u].ch[c]=pn;
    139             pn++;
    140         }
    141         
    142         u=trie[u].ch[c];
    143     }
    144     
    145     trie[u].val++;
    146 }
    147 
    148 inline void bfs(){
    149     int u,v;
    150     
    151     q.clear();
    152     
    153     for(ri i=0;i<26;i++)
    154         if(trie[0].ch[i])
    155             q.push(trie[0].ch[i]);
    156     
    157     while(!q.emp()){
    158         u=q.top();
    159         q.pop();
    160         
    161         for(ri i=0;i<26;i++){
    162             v=trie[u].ch[i];
    163             
    164             if(!v){
    165                 trie[u].ch[i]=trie[trie[u].next].ch[i];
    166                 continue;
    167             }
    168             
    169             q.push(v);
    170             trie[v].next=trie[trie[u].next].ch[i];
    171             if(trie[trie[v].next].val)
    172                 trie[v].last=trie[v].next;
    173             else
    174                 trie[v].last=trie[trie[v].next].last;
    175         }
    176     }
    177 }
    178 
    179 void work(int now){
    180     if(now){
    181         ans+=trie[now].val;
    182         trie[now].val=0;
    183         
    184         work(trie[now].last);
    185     }
    186 }
  • 相关阅读:
    Linux组件封装(三) Thread的封装
    Linux组件封装(二) 条件变量Condition的封装
    Linux组件封装(一) 互斥锁MutexLock
    迭代器适配器(二) general inserter的简单实现
    迭代适配器(一) back_insert和front_insert的简单实现
    priority_queue的简单实现
    svm简易例子——matlab版本
    讲座 on the connection between vision and language
    svm相关理解
    为什么深度学习有效?(why deep learning works)
  • 原文地址:https://www.cnblogs.com/running-coder-wfh/p/7965866.html
Copyright © 2011-2022 走看看