zoukankan      html  css  js  c++  java
  • AC自动机修正

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=1000000+10,sig=2;
    11 struct ACauto{
    12     struct node{node*ch[sig],*fail;int cnt;char x;}AC[maxn],*root;int nodecnt;
    13     int id[156];queue<node*>RAM;
    14     node*newnode(){
    15         node*t;
    16         if(!RAM.empty()) t=RAM.front();
    17         else t=&AC[++nodecnt];
    18         //t->fail=root;
    19         t->cnt=0;
    20         return t;
    21     }
    22     void init(){
    23         for(int i=0;i<sig;i++) id[i+'a']=i;
    24         root=&AC[0];root->cnt=0;root->x='#';nodecnt=0;return;
    25     }
    26     void insert(char*s){
    27         int len=strlen(s);node*x=root;
    28         for(int i=0;i<len;i++){
    29             int c=id[s[i]];
    30             if(!x->ch[c]) x->ch[c]=newnode();
    31             x=x->ch[c];x->x=s[i];
    32         } x->cnt++;return;
    33     }
    34     void getfail(){
    35         queue<node*>Q;Q.push(root);
    36         while(!Q.empty()){
    37             node*x=Q.front();Q.pop();
    38             for(int c=0;c<sig;c++){
    39                 if(x->ch[c]){
    40                     node*p=x->fail;while(p&&!p->ch[c])p=p->fail;
    41                     x->ch[c]->fail=p?p->ch[c]:root;Q.push(x->ch[c]);
    42                 } else x->ch[c]=x==root?root:x->fail->ch[c];
    43             }
    44         } return;
    45     }
    46     int query(char*s){
    47         int len=strlen(s);node*x=root,*p;int sum=0;
    48         for(int i=0;i<len;i++){
    49             int c=id[s[i]];x=x->ch[c];
    50             for(node*p=x;p&&p->cnt;p=p->fail) sum++;
    51         } return sum;
    52     }
    53 }sol;
    54 inline int read(){
    55     int x=0,sig=1;char ch=getchar();
    56     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
    57     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
    58     return x*=sig;
    59 }
    60 inline void write(int x){
    61     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    62     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    63     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    64 }
    65 char s[1000010],t[1000010];
    66 void init(){
    67     scanf("%s",s);
    68     scanf("%s",t);
    69     sol.init();
    70     sol.insert(s);
    71     sol.getfail();
    72     write(sol.query(t));
    73     return;
    74 }
    75 void work(){
    76     return;
    77 }
    78 void print(){
    79     return;
    80 }
    81 int main(){init();work();print();return 0;}
  • 相关阅读:
    从零开始的ESP8266探索(1)-使用Server功能搭建Web Server
    模型收集
    3D打印社区
    [教程]教你如何制作彩色的3D打印Groot
    1-5 软件安装
    1-4 打印机测试
    1-3 打印机几个重要操作
    1-1 打印机基本参数
    1-2 打印机使用注意事项
    (二 -3-3) 天猫精灵接入Home Assistant-自动发现Mqtt设备-自动生成配置信息
  • 原文地址:https://www.cnblogs.com/chxer/p/4628472.html
Copyright © 2011-2022 走看看