zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    深信服入职前编码训练21题--02
    深信服入职前编码训练21题--01
    Leetcode本地阅读器开发--01界面设计三
    Leetcode本地阅读器开发--01界面设计二
    Leetcode本地阅读器开发--01界面设计一
    使用DDMS测试安卓手机APP的性能(android)
    在PC上测试移动端网站和模拟手机浏览器的5大方法
    SeleniumIDE与eclipse如何连接使用
    Selenium RC配置
    Selenium IDE- 不同的浏览器
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4942766.html
Copyright © 2011-2022 走看看