zoukankan      html  css  js  c++  java
  • LA_4670_Dominating_Patterns_(AC自动机+map)

    描述


    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

    给出一个字符串和一些子串,求其中出现次数最多的子串.

    分析


    在AC自动机上面跑就行了.但是有一个要注意的地方,就是在输入文件里同一个子串重复出现.如果不特殊处理的话,后一个子串就会把Trie里的前一个子串覆盖掉.,所以我们可以用个map...

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=150+5,maxl=1e6+5,maxnode=150*70+5;
     5 int n;
     6 char text[maxl],p[maxn][70+5];
     7 map <string,int> ms;
     8 struct Aho_Corasick{
     9     int ch[maxnode][26];
    10     int f[maxnode],val[maxnode],last[maxnode],cnt[maxn];
    11     int sz;
    12     inline int idx(char c){ return c-'a'; }
    13     void init(){
    14         sz=1;
    15         memset(ch[0],0,sizeof ch[0]);
    16         memset(cnt,0,sizeof cnt);
    17         ms.clear();
    18     }
    19     void insert(char *s,int v){
    20         ms[string(s)]=v;
    21         int u=0;
    22         for(;*s;s++){
    23             int c=idx(*s);
    24             if(!ch[u][c]){
    25                 memset(ch[++sz],0,sizeof ch[0]);
    26                 val[sz]=0;
    27                 ch[u][c]=sz;
    28             }
    29             u=ch[u][c];
    30         }
    31         val[u]=v;
    32     }
    33     void get_fail(){
    34         queue <int> q;
    35         f[0]=0;
    36         for(int c=0;c<26;c++){
    37             int u=ch[0][c];
    38             if(u){ f[u]=0; q.push(u); }
    39         }
    40         while(!q.empty()){
    41             int r=q.front(); q.pop();
    42             for(int c=0;c<26;c++){
    43                 int u=ch[r][c];
    44                 if(!u){ ch[r][c]=ch[f[r]][c]; continue; }
    45                 q.push(u);
    46                 int v=f[r];
    47                 f[u]=ch[v][c];
    48                 last[u]=val[f[u]]?f[u]:last[f[u]];
    49             }
    50         }
    51     }
    52     void work(int j){
    53         if(j){
    54             cnt[val[j]]++;
    55             work(last[j]);
    56         }
    57     }
    58     void find(char *T){
    59         int j=0;
    60         for(;*T;T++){
    61             int c=idx(*T);
    62             while(j&&!ch[j][c]) j=f[j];
    63             j=ch[j][c];
    64             if(val[j]) work(j);
    65             else work(last[j]);
    66         }
    67     }
    68 }ac;
    69 int main(){
    70     while(scanf("%d",&n)&&n){
    71         ac.init();
    72         for(int i=1;i<=n;i++){
    73             scanf("%s",p[i]);
    74             ac.insert(p[i],i);
    75         }
    76         ac.get_fail();
    77         scanf("%s",text);
    78         ac.find(text);
    79         int best=-1;
    80         for(int i=1;i<=n;i++) best=max(best,ac.cnt[i]);
    81         printf("%d
    ",best);
    82         for(int i=1;i<=n;i++) if(ac.cnt[ms[string(p[i])]]==best) printf("%s
    ",p[i]);
    83     }
    84     return 0;
    85 }
    View Code

    4670
    Dominating Patterns
    The archaeologists are going to decipher a very mysterious “language”. Now, they know many language
    patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string,
    these patterns may appear more than one times in a large text string (also only lower case English
    letters).
    What matters most is that which patterns are the dominating patterns. Dominating pattern is the
    pattern whose appearing times is not less than other patterns.
    It is your job to find the dominating pattern(s) and their appearing times.
    Input
    The entire input contains multi cases. The first line of each case is an integer, which is the number of
    patterns N , 1 ≤ N ≤ 150. Each of the following N lines contains one pattern, whose length is in range
    [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up
    to 10 6 .
    At the end of the input file, number ‘0’ indicates the end of input file.
    Output
    For each of the input cases, output the appearing times of the dominating pattern(s). If there are more
    than one dominating pattern, output them in separate lines; and keep their input order to the output.
    Sample Input
    2
    aba
    bab
    ababababac
    6
    beta
    alpha
    haha
    delta
    dede
    tata
    dedeltalphahahahototatalpha
    0
    Sample Output
    4
    aba
    2
    alpha
    haha

  • 相关阅读:
    poj 2674 Linear world
    poj 3185 The Water Bowls
    The Largest Clique (uva11324)
    Proving Equivalences (LA 4287)
    强联通分量( HihoCoder 1185 )
    求点双联通分量(HihoCoder
    求桥,割点(HihoCoder
    欧拉回路
    uva10054
    表达式树(公共表达式消除 uva 12219)
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5550643.html
Copyright © 2011-2022 走看看