zoukankan      html  css  js  c++  java
  • hdu 2222

    AC自动机入门题,只是上来传个模板。。。

     1 //#include<bits/stdc++.h>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<iostream>
     6 #include<queue>
     7 #define inc(i,l,r) for(int i=l;i<=r;i++)
     8 #define dec(i,l,r) for(int i=l;i>=r;i--)
     9 #define link(x) for(edge *j=h[x];j;j=j->next)
    10 #define mem(a) memset(a,0,sizeof(a))
    11 #define inf 1e9
    12 #define ll long long
    13 #define succ(x) (1<<x)
    14 #define lowbit(x) (x&(-x))
    15 #define NM 10005
    16 using namespace std;
    17 int read(){
    18     int x=0,f=1;char ch=getchar();
    19     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    20     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    21     return x*f;
    22 }
    23 struct node{
    24     node *c[26],*fail;
    25     int f;
    26 }N[50*NM],*o,*root;
    27 node* newnode(){
    28     o->fail=root;o->f=false;return o++;
    29 }
    30 int n,ans,m,T;
    31 char st[1000000+5];
    32 void ins(){
    33     scanf("%s",st);m=strlen(st)-1;
    34     node *r=root;
    35     inc(i,0,m){
    36         int v=st[i]-'a';
    37         if(!r->c[v])r->c[v]=newnode();
    38         r=r->c[v];
    39     }
    40     r->f++;
    41 }
    42 void bfs(){
    43     queue<node*>q;
    44     inc(i,0,25)if(root->c[i])q.push(root->c[i]);
    45     while(!q.empty()){
    46         node *t=q.front();q.pop();
    47         inc(j,0,25)if(t->c[j]){
    48             node *r=t->fail;
    49             while(r!=root&&!r->c[j])r=r->fail;
    50             t->c[j]->fail=r->c[j]?r->c[j]:root;
    51         //    if(t->c[j]->fail->f)t->c[j]->f+=r->c[j]->fail->f;
    52             q.push(t->c[j]);
    53         }
    54     }
    55 }
    56 void find(){
    57     scanf("%s",st);m=strlen(st)-1;
    58     node *r=root;
    59     inc(i,0,m){
    60         int v=st[i]-'a';
    61         while(r!=root&&!r->c[v])r=r->fail;
    62         if(r->c[v]){
    63             r=r->c[v];
    64             for(node *t=r;t!=root;t=t->fail)
    65             ans+=t->f,t->f=0;
    66         }
    67     }
    68 }
    69 int main(){
    70 //    freopen("data.in","r",stdin);
    71     T=read();
    72     while(T--){
    73         mem(N);o=N;root=newnode();root->fail=root;ans=0;
    74         n=read();
    75         inc(i,1,n)ins();
    76         bfs();
    77         find();
    78         printf("%d
    ",ans);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    osworkflow
    用Flash做报表,推荐使用Flash饼图
    ANT 发布项目中 build.xml 文件的详细配置
    tomcat 修改java后不重启的方法
    工厂方法(Factory Method)模式
    NSRunLoop概述和原理
    使用NSOperationQueue简化多线程开发
    使用Grad Central Dispatch简化iPhone开发
    进度显示例子学习
    深入浅出 iOS 之多线程
  • 原文地址:https://www.cnblogs.com/onlyRP/p/5192860.html
Copyright © 2011-2022 走看看