• UVALive 4670 Dominating Patterns


    Dominating Patterns

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVALive. Original ID: 4670
    64-bit integer IO format: %lld      Java class name: Main

    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 N1$ le$N$ le$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 106.

    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
    

    Source

     
    解题:Trie图或者AC自动机
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 500;
     4 char str[maxn][maxn];
     5 map<string,int>mp;
     6 struct Trie{
     7     int ch[maxn*maxn][26],fail[maxn*maxn],cnt[maxn*maxn],tot,ret;
     8     bool flag[maxn*maxn];
     9     void init(){
    10         ret = tot = 0;
    11         newnode();
    12     }
    13     int newnode(){
    14         memset(ch[tot],0,sizeof ch[tot]);
    15         fail[tot] = cnt[tot] = 0;
    16         flag[tot] = false;
    17         return tot++;
    18     }
    19     void insert(char *str,int root = 0){
    20         for(int i = 0; str[i]; ++i){
    21             int &x =  ch[root][str[i]-'a'];
    22             if(!x) x = newnode();
    23             root = x;
    24         }
    25         mp[str] = root;
    26         flag[root] = true;
    27     }
    28     void build(int rt = 0){
    29         queue<int>q;
    30         for(int i = 0; i < 26; ++i)
    31             if(ch[rt][i]) q.push(ch[rt][i]);
    32         while(!q.empty()){
    33             rt = q.front();
    34             q.pop();
    35             for(int i = 0; i < 26; ++i){
    36                 int &x = ch[rt][i],y = ch[fail[rt]][i];
    37                 if(x){
    38                     fail[x] = y;
    39                     q.push(x);
    40                 }else x = y;
    41             }
    42         }
    43     }
    44     void count(char *str,int rt = 0){
    45         for(int i = 0; str[i]; ++i){
    46             int x = rt = ch[rt][str[i]-'a'];
    47             while(x && flag[x]){
    48                 ret = max(ret,++cnt[x]);
    49                 x = fail[x];
    50             }
    51         }
    52     }
    53 }ac;
    54 char ss[1000100];
    55 int main(){
    56     int n;
    57     while(scanf("%d",&n),n){
    58         ac.init();
    59         mp.clear();
    60         for(int i = 0; i < n; ++i){
    61             scanf("%s",str[i]);
    62             ac.insert(str[i]);
    63         }
    64         ac.build();
    65         scanf("%s",ss);
    66         ac.count(ss);
    67         printf("%d
    ",ac.ret);
    68         for(int i = 0; i < n; ++i)
    69             if(ac.cnt[mp[str[i]]] == ac.ret) puts(str[i]);
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    第七周课程总结&实验报告(五)
    2020软件工程作业02
    自我介绍
    2019学期总结
    2019 第二次实验报告
    git 小错误
    12
    2019第十一周作业
    第十周作业
    第九周
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4942766.html
Copyright © 2020-2023  润新知