zoukankan      html  css  js  c++  java
  • bzoj 3172: [Tjoi2013]单词

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #define M 1000008
     5 using namespace std;
     6 char ch[M];
     7 int a[M][27],n,cnt=1,fa[M],f1[M],ans[M],q[M];
     8 long long sum[M],aa;
     9 void jia()
    10 {
    11     int now=1;
    12     for(int i=1;i<=strlen(ch+1);i++)
    13       if(!a[now][ch[i]-96])
    14         {
    15             cnt++;
    16             a[now][ch[i]-96]=cnt;
    17             sum[cnt]++;
    18             now=cnt;
    19         }
    20       else
    21         {
    22             now=a[now][ch[i]-96];
    23             sum[now]++;
    24         }
    25     ans[0]++;
    26     ans[ans[0]]=now;
    27 }
    28 void build()
    29 {
    30     int h=0,t=1;
    31     q[1]=1;
    32     for(;h<t;)
    33       {
    34         int p=q[++h];
    35         for(int i=1;i<=26;i++)
    36           if(a[p][i])
    37             {
    38                 int now=fa[p];
    39                 q[++t]=a[p][i];
    40                 for(;!a[now][i];now=fa[now]);
    41                 fa[a[p][i]]=a[now][i];
    42             }
    43       }
    44     return;
    45 }
    46 void dfs(int a1)
    47 {
    48     f1[a1]=1;
    49     int now;
    50     for( now=fa[a1];f1[now]&&now>1;sum[now]+=sum[a1],now=fa[now]);
    51     sum[now]+=sum[a1];
    52     for(int i=1;i<=26;i++)
    53       if(a[a1][i])
    54         dfs(a[a1][i]);
    55     return;
    56 }
    57 int main()
    58 {
    59     scanf("%d",&n);
    60     for(int i=1;i<=n;i++)
    61       {
    62         scanf("%s",ch+1);
    63         jia();
    64       }
    65     for(int i=1;i<=26;i++)
    66       a[0][i]=1;
    67     build();
    68     dfs(1);
    69     for(int i=1;i<=n;i++)
    70       printf("%lld
    ",sum[ans[i]]);
    71     return 0;
    72 }

    AC自动机

  • 相关阅读:
    牛哄哄的 汉诺塔递归
    c 终端控制
    ubuntu apt源
    vim 用法
    纯js 判断表单为空 阻止 input 提交
    Yii2 文本框前加图标 input 添加属性的方法
    layer 弹窗 弹出层 iframe子页面获取父级页面id
    获取Yii2 常用 路径 目录 url
    Yii Activeform 表单
    Yii use namespace 命名空间
  • 原文地址:https://www.cnblogs.com/xydddd/p/5309134.html
Copyright © 2011-2022 走看看