当前位置:编程学习 > C/C++ >>

UVA 1449 Dominating Patterns (LA4670) 出现次数最多的子串 ac自动机

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, 1N150. 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

题意:
有n个小写字母组成的字符床和一个文本串   你的任务是找出哪些字符串在文本中出现的次数最多   例如  aba 在ababa中出现2次  但是bab只出现了一次

输入n  之后n个字符串   长度为1-70   n小于等于150  之后一个文本串  长度最长为10的6次方   
输出出现最多的次数 以及出现最多的字符串为什么  如果存在多个  按输入顺序排列

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36265


思路:
很明显的ac自动机  
[cpp]
#include<stdio.h>   #include<string.h> 
 #include<malloc.h>   #include<queue>
   #include<set>   using namespace std;
 struct node  {      int count,id; 
    struct node *next[26];    
 struct node *fail;    
 void init()      {          int i;  
       for(i=0;i<26;i++)              next[i]=NULL; 
        count=0;  
       fail=NULL;    
     id=-1;      }  }*root;
  void insert(char *str,int id)  { 
    int len,k;   
  node *p=root;
     len=strlen(str); 
    for(k=0;k<len;k++)    
 {          int pos=str[k]-'a';  
       if(p->next[pos]==NULL)          {              p->next[pos]=new node;       
      p->next[pos]->init();              p=p->next[pos];    
     }          else              p=p->next[pos]; 
    }      p->count++;   
  p->id=id;  }  void getfail()  {      int i; 
       node *p=root,*son,*temp;    
    queue<struct node *>que;   
     que.push(p);     
   while(!que.empty())         {             temp=que.front();  
          que.pop();             for(i=0;i<26;i++)      
      {                 son=temp->next[i];       
         if(son!=NULL)                 {       
             if(temp==root) {son->fail=root;}          
          else                     {                    
    p=temp->fail;                         while(p)         
               {                             if(p->next[i])         
                   {                                 son->fail=p->next[i];      
                          break;                             }                             p=p->fail;         
               }                    
    if(!p)  son->fail=root;                     }                     que.push(son);   
             }           &nbs

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,