zoukankan      html  css  js  c++  java
  • luogu 3796 【模板】AC自动机(加强版)

    我太菜了 

    棒神%%%

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstdlib>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<vector>
     8 #include<queue>
     9 #define inf 2139062143
    10 #define ll long long
    11 #define MAXN 1010101
    12 using namespace std;
    13 inline int read()
    14 {
    15     int x=0,f=1;char ch=getchar();
    16     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    17     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 char str[MAXN],s[155][77];
    21 int sz,n,ans[155];
    22 struct Trie {int to,ch[26],fail;}tr[12000];
    23 void insert(int len,int x)
    24 {
    25     int pos=0;
    26     for(int i=1;i<=len;i++)
    27     {
    28         if(!tr[pos].ch[s[x][i]-'a']) tr[pos].ch[s[x][i]-'a']=++sz;
    29         pos=tr[pos].ch[s[x][i]-'a'];
    30     }
    31     tr[pos].to=x;
    32 }
    33 void build()
    34 {
    35     queue <int> q;
    36     for(int i=0;i<26;i++)
    37         if(tr[0].ch[i]) q.push(tr[0].ch[i]);
    38     while(!q.empty())
    39     {
    40         int k=q.front();q.pop();
    41         for(int i=0;i<26;i++)
    42             if(tr[k].ch[i]) tr[tr[k].ch[i]].fail=tr[tr[k].fail].ch[i],q.push(tr[k].ch[i]);
    43             else tr[k].ch[i]=tr[tr[k].fail].ch[i];
    44     }
    45 }
    46 void query(int len)
    47 {
    48     int k=0;
    49     for(int i=1;i<=len;i++)
    50     {
    51         k=tr[k].ch[str[i]-'a'];
    52         for(int t=k;t;t=tr[t].fail) ans[tr[t].to]++;
    53     }
    54 }
    55 int main()
    56 {
    57     int res=0;
    58     while(scanf("%d",&n))
    59     {
    60         if(!n) return 0;
    61         for(int i=0;i<12000;i++) tr[i].to=0,memset(tr[i].ch,0,sizeof(tr[i].ch)),ans[i]=tr[i].fail=0;
    62         res=sz=0;//memset(ans,0,sizeof(ans)),
    63         for(int i=1;i<=n;i++) scanf("%s",s[i]+1),insert(strlen(s[i]+1),i);
    64         build();
    65         scanf("%s",str+1);
    66         query(strlen(str+1));
    67         for(int i=1;i<=n;i++) res=max(res,ans[i]);
    68         printf("%d
    ",res);
    69         for(int i=1;i<=n;i++)
    70             if(ans[i]==res) printf("%s
    ",s[i]+1);
    71     }
    72 }
    View Code
  • 相关阅读:
    茶香账簿小程序开发进度(3)
    软件需求十步走阅读笔记(一)
    茶香账簿小程序开发进度(2)
    茶香账簿小程序开发进度(1)
    软件工程概论总结及对老师的意见
    人月神话阅读笔记03
    第二阶段第七次站立会议
    第二阶段第六次站立会议
    第二阶段第五次站立会议
    第二阶段第四次站立会议
  • 原文地址:https://www.cnblogs.com/yyc-jack-0920/p/8177205.html
Copyright © 2011-2022 走看看