zoukankan      html  css  js  c++  java
  • [UVA1149]Dominating Patterns

    Description

     

    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$ 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

     AC自动机模版题,注意给的数据末位有一个空格…freopen的时候小心了……

      1 #include <algorithm>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <iostream>
      6 #include <vector>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <climits>
     12 #include <cmath>
     13 
     14 using namespace std;
     15 
     16 inline int GetId(char c) {
     17     return c - 'a';
     18 }
     19 
     20 char str[1000010];
     21 char ss[200][110];
     22 int cnt[1000];
     23 int n;
     24 int ans;
     25 
     26 typedef class Node {
     27 public:
     28     Node *next[26];
     29     Node *fail;
     30     int id;
     31     int cnt;
     32     Node(){
     33         for(int i = 0; i < 26; i++) {
     34             next[i] = NULL;
     35         }
     36         fail = NULL;
     37         id = -1;
     38         cnt = 0;
     39     }
     40 }Node;
     41 
     42 class AC_Automation {
     43 public:
     44     Node *root;
     45     queue <Node*> q;
     46     AC_Automation() {
     47         root = new Node;
     48         while(!q.empty()) {
     49             q.pop();
     50         }
     51     }
     52     void insert(char *s, int th) {
     53         Node *cur = root;
     54         int idx;
     55         for(int i = 0; s[i]; i++) {
     56             idx = GetId(s[i]);
     57             if(!cur->next[idx]) {
     58                 cur->next[idx] = new Node();
     59             }
     60             cur = cur->next[idx];
     61         }
     62         cur->id = th;
     63         cur->cnt++;
     64     }
     65     void BuildAC() {
     66         Node *cur,*tmp;
     67         q.push(root);
     68         while(!q.empty()) {
     69             cur = q.front();
     70             q.pop();
     71             for(int i = 0; i < 26; i++) {
     72                 if(cur->next[i]) {
     73                     if(cur == root) {
     74                         cur->next[i]->fail = root;
     75                     }
     76                     else {
     77                         tmp = cur->fail;
     78                         while(tmp->fail && !tmp->next[i]) {
     79                             tmp = tmp->fail;
     80                         }
     81                         if(tmp->next[i]) {
     82                             cur->next[i]->fail = tmp->next[i];
     83                         }   
     84                         else {
     85                             cur->next[i]->fail = root;
     86                         }
     87                     }
     88                     q.push(cur->next[i]);
     89                 }
     90             }
     91         }
     92     }
     93     void query(char *s) {
     94         Node *p = root;
     95         Node *tmp;
     96         char x;
     97         for(int i = 0; s[i]; i++) {
     98             x = GetId(s[i]);
     99             while(!p->next[x] && p != root) {
    100                 p = p->fail;
    101             }
    102             p = p->next[x];
    103             if(!p) {
    104                 p = root;
    105             }
    106             tmp = p;
    107             while(tmp != root) {
    108                 if(tmp->cnt >= 1) {                
    109                     if(tmp->id != -1) {
    110                         cnt[tmp->id]++;
    111                     }
    112                 }
    113                 tmp = tmp->fail;
    114             }
    115         }
    116     }
    117 };
    118 
    119 int main() {
    120     // freopen("in", "r", stdin);
    121     while(~scanf("%d", &n) && n) {
    122         memset(cnt, 0, sizeof(cnt));
    123         memset(str, 0, sizeof(str));
    124         memset(ss, 0, sizeof(ss));
    125         getchar();
    126         AC_Automation ac;
    127         ans = -1;
    128         for(int i = 0; i < n; i++) {
    129             gets(ss[i]);
    130             ac.insert(ss[i], i);
    131         }
    132         ac.BuildAC();
    133         gets(str);
    134         ac.query(str);
    135         for(int i = 0; i < n; i++) {
    136             if(cnt[i] > ans) {
    137                 ans = cnt[i];
    138             }
    139         }
    140         printf("%d
    ", ans);
    141         for(int i = 0; i < n; i++) {
    142             if(ans == cnt[i]) {
    143                 printf("%s
    ", ss[i]);
    144             }
    145         }
    146     }
    147 }
  • 相关阅读:
    SQL索引是什么?索引的作用是什么?索引的优点是什么?索引的缺点是什么?索引的分类?什么情况下该创建索引?
    Eclipse中JSP生成的class文件去了哪里?
    Jboss 遇到的问题 :org.jboss.tools.vpe.xulrunner.XulRunnerBundleNotFoundException: Bundle org.mozilla.xulrunner.win32.win32.x86_64 is not found.
    Java框架之Hibernate实战篇
    《从0开始学架构》学习笔记(一)
    JavaWeb——Servlet(详细笔记)
    用Eclipse+Maven+Jetty构建Java Web开发环境(详细笔记)
    Jetty 安装、启动与项目部署
    快速排序相关(学习笔记)
    Java中对象的比较(学习笔记)
  • 原文地址:https://www.cnblogs.com/kirai/p/4766922.html
Copyright © 2011-2022 走看看